Blog>
Snippets

Initializing TanStack Store in a React Project

Demonstrate how to set up TanStack Store in a React application, including creating the initial store and wrapping the app component for state access.
import { createQueryClientProvider } from '@tanstack/react-query';
const queryClient = new QueryClient();
export const QueryClientProvider = createQueryClientProvider(queryClient);
This piece of code imports the necessary function from the TanStack react-query package to create a QueryClientProvider using a new instance of QueryClient. This is the initial store setup, responsible for managing the state and cache of the queries in the application.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { QueryClientProvider } from './queryClient';

ReactDOM.render(
  <QueryClientProvider>
    <App />
  </QueryClientProvider>,
  document.getElementById('root')
);
This code wraps the App component with the QueryClientProvider that was set up in the previous step. It ensures that any component within the App can have access to the store's state and caching abilities. This is crucial for enabling global state management in React applications using TanStack.