
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-query-class-component
Advanced tools
A HOC Utility tool built for react-query, through this you can use react-query hooks to fetch and pass data in your React class based components.
Install package in your project
npm i react-query-class-component
// To pass query data globally
import {QueryClientClassProvider, withQueryClient} from "react-query-class-component";
// To use react-query hook in class component
import {QueryClientHook} from "react-query-class-component";
It supports almost all the hooks, below are the examples of few ones:
import React from 'react';
import { QueryClient, QueryClientProvider, useQuery } from "react-query";
import { QueryClientHook } from 'react-query-class-component';
const queryClient = new QueryClient();
function App() {
return (
<QueryClientProvider client={queryClient}>
<TodoList/>
</QueryClientProvider>
);
}
class TodoList extends React.Component {
render() {
return (
<QueryClientHook
hook={useQuery} // react query hook
params={
[
'todos', // keyName
() => { // query function
return fetch('https://jsonplaceholder.typicode.com/todos').then(res => res.json());
},
// ...options
]
}>
{({data, isLoading}) => {
if (isLoading) return <h1>Loading</h1>;
return (
<div className="App">
<h2>Todo list</h2>
{
data.map((query, key) => {
return <li key={key}>{query?.title}</li>;
})
}
</div>
);
}}
</QueryClientHook>
)
}
}
import React from 'react';
import {QueryClient, QueryClientProvider, useQueries, useQuery} from "react-querynnnt";
import {QueryClientClassProvider, withQueryClient} from 'react-query-class-component';
const queryClient = new QueryClient();
export const AppRoot = () => {
return (
<QueryClientProvider client={queryClient}>
<QueryClientClassProvider queries={{
// useQuery example
todoSingle: {
hook: useQuery,
params: ['todos', () => {
return fetch('https://jsonplaceholder.typicode.com/todos').then(res => res.json())
}],
},
// with options
todoSingleDisableRefetch: {
hook: useQuery,
params: [
{
queryKey: 'todos-disable-refetch',
queryFn: () => {
return fetch('https://jsonplaceholder.typicode.com/todos').then(res => res.json())
},
// you can mention options here...
refetchOnWindowFocus: false,
}
],
},
// useQueries example
todoMulti: {
hook: useQueries,
params: [
[
{
queryKey: ['post', 1], queryFn: () => {
return fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json());
}
},
{
queryKey: ['post', 2], queryFn: () => {
return fetch('https://jsonplaceholder.typicode.com/todos/2').then(res => res.json());
}
},
]
],
}
}}>
<App/>
</QueryClientClassProvider>
</QueryClientProvider>
);
}
Once you have finished passing queries in provider, then you can wrap your component with withQueryClient method to get queries result in reactQueries prop variable.
class TodoList extends React.Component {
render() {
const {reactQueries} = this.props;
// for ya ;)
console.log('debug', reactQueries);
// you can access data using same id you you defined in QueryClientClassProvider queries props.
if (reactQueries?.todoSingle?.isLoading) {
return 'Loading data...';
}
// reperesent data
return (
<>
{reactQueries.todoSingle?.data.map((query, key) => {
return <li key={key}>{query?.title}</li>;
})}
</>
)
}
}
export default TodoList = withQueryClient(TodoList);
To use react-query hook in class component
hook?: any
params?: any
It should be placed within QueryClientProvider
queries?: any
queries={{
'fetch-1': { // mention id here
hook: useQuery, // react-query hook
params: [ // list of params to be passed in hook mentioned above.
{
queryKey: 'todos-disable-refetch',
queryFn: () => {
return fetch('https://jsonplaceholder.typicode.com/todos').then(res => res.json())
},
// options
refetchOnWindowFocus: false,
}
],
},
'fetch-2': { // mention id here
hook: useQuery, // react-query hook
params: [ // list of params to be passed in hook mentioned above.
{
queryKey: 'todos-disable-refetch',
queryFn: () => {
return fetch('https://jsonplaceholder.typicode.com/todos').then(res => res.json())
},
// options
refetchOnWindowFocus: false,
}
],
},
}}
To get queries result in your class component you will need to wrap your class component with this method, then you
access queries using this.props.reactQuries prop variable.

Please raise issue or sent Pull Request if you find any bugs in package.
FAQs
Make react-query available for class based components
The npm package react-query-class-component receives a total of 0 weekly downloads. As such, react-query-class-component popularity was classified as not popular.
We found that react-query-class-component demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.