Blog>
Snippets

Setting Up TanStack Store in a React Project

Demonstrate how to install TanStack Store, configure the QueryClient, and wrap a React application with QueryClientProvider for global access.
$ npm i @tanstack/react-query
# or
$ yarn add @tanstack/react-query
First, install the TanStack React Query package using npm or yarn. This allows you to fetch, cache, and update data in your React applications efficiently.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

// Create a client
const queryClient = new QueryClient();
Import QueryClient and QueryClientProvider from the TanStack React Query package. Then, instantiate QueryClient. This object is used for configuring defaults, managing the cache, triggering background updates, and more.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

ReactDOM.render(
  <QueryClientProvider client={queryClient}>
    <App />
  </QueryClientProvider>,
  document.getElementById('root')
);
Wrap your application's root component with QueryClientProvider and pass the queryClient instance to it. This makes the React Query functionality available throughout your entire application.