Setting Up React Query in a React App
Demonstrate how to integrate React Query into a React application by configuring the React QueryClient and providing it to the application using the QueryClientProvider.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
First, import QueryClient and QueryClientProvider from '@tanstack/react-query'.
const queryClient = new QueryClient();
Next, instantiate a new QueryClient. This object is used by React Query to manage queries and cache.
import ReactDOM from 'react-dom';
import App from './App'; // Assuming your main app component is App.jsx
Import ReactDOM to render the application and import the App component, which is the root of your React application.
ReactDOM.render(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>,
document.getElementById('root')
);
Finally, wrap the <App /> component with <QueryClientProvider> and provide the queryClient instance. This makes React Query available throughout your application.