添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爽快的墨镜  ·  能否直接将React ...·  6 天前    · 
骑白马的饭卡  ·  How to checkout a tag ...·  3 天前    · 
威武的长颈鹿  ·  getDeclaredMethod()和ge ...·  2 年前    · 
活泼的领结  ·  Unity控制台显示C++ ...·  2 年前    · 
温文尔雅的蘑菇  ·  Running cells with ...·  2 年前    · 
坚韧的竹笋  ·  冲突@Dragenter ...·  3 年前    · 

No QueryClient set, use QueryClientProvider to set one [Fix]

avatar
Borislav Hadzhiev

Last updated: May 9, 2023
4 min

banner

# No QueryClient set, use QueryClientProvider to set one [Fix]

The React Query error "No QueryClient set, use QueryClientProvider to set one" occurs when you forget to wrap your React application in a QueryClientProvider component.

To solve the error, wrap the entry point of your application (e.g. the App component) in a QueryClientProvider .

no queryclient set use queryclientprovider to set one

Here is an example that shows how you should wrap your main component in a QueryClientProvider .

App.js
import React from 'react'; import { QueryClient, QueryClientProvider, useQuery, } from 'react-query'; const queryClient = new QueryClient(); export default function App() { return ( <QueryClientProvider client={queryClient}> <Users /> </QueryClientProvider> function Users() { return ( // ... your users component here

Note: if you use React-Query v4, you have to import from @tanstack/react-query instead.

App.js
import { QueryClient, QueryClientProvider, useQuery, } from '@tanstack/react-query';

Notice that I wrapped the main component ( App in this case) in a QueryClientProvider .

App.js
import { QueryClient, QueryClientProvider } from 'react-query' const queryClient = new QueryClient() function App() { return ( <QueryClientProvider client={queryClient}> <YourMainComponent /> </QueryClientProvider>

The QueryClientProvider component is used to connect a QueryClient to your application.

Therefore all of the components that use the useQuery hook have to be located somewhere under a QueryClientProvider .

The QueryClientProvider component takes a client prop.

The clientProp is required and must be set to a QueryClient (the QueryClient instance to provide).

Here is a complete example that fetches data from a remote API using React query.

App.js
import React from 'react'; import { QueryClient, QueryClientProvider, useQuery, } from 'react-query'; const queryClient = new QueryClient(); export default function App() { // 👇️ Wrap with QueryClientProvider here return ( <QueryClientProvider client={queryClient}> <Users /> </QueryClientProvider> function Users() { // 👇️ can use the useQuery hook here const {isLoading, error, data} = useQuery({ queryKey: ['usersData'], queryFn: () => fetch('https://randomuser.me/api').then(res => res.json()), }); if (isLoading) return 'Loading...'; if (error) return 'An error has occurred: ' + error.message; console.log(data); return ( {data.results.map(user => ( <div key={user.id.value}> Name: {user.name.first} {user.name.last} <h2>Gender: {user.gender}</h2> <h2>Phone: {user.phone}</h2> <h2>Email: {user.email}</h2> </div> ))} </div>

successfully fetch data using react query

Note: if you use React-Query v4, you have to import from @tanstack/react-query instead.

App.js
import { QueryClient, QueryClientProvider, useQuery, } from '@tanstack/react-query';

# Wrapping your App component with a QueryClientProvider in index.js

An alternative approach is to wrap your App component with a QueryClientProvider in your index.js file.

Your index.js file is the entry point of your React.js application, so all components in your React app will be able to use the useQuery hook.

Here is the code for the index.js file.

index.js
import React from 'react'; import ReactDOM from 'react-dom/client'; import App from './App'; import {QueryClient, QueryClientProvider} from 'react-query'; const queryClient = new QueryClient(); const root = ReactDOM.createRoot( document.getElementById('root'), root.render( <React.StrictMode> <QueryClientProvider client={queryClient}> <App /> </QueryClientProvider> </React.StrictMode>,

Note: if you use React-Query v4, you have to import from @tanstack/react-query instead.

App.js
import { QueryClient, QueryClientProvider, useQuery, } from '@tanstack/react-query';

We wrapped the App component in a QueryClientProvider and rendered the result.

Here is the code for the App component.

App.js
import React from 'react'; import {useQuery} from 'react-query'; export default function App() { return <Users />; function Users() { const {isLoading, error, data} = useQuery({ queryKey: ['usersData'], queryFn: () => fetch('https://randomuser.me/api').then(res => res.json()), }); if (isLoading) return 'Loading...'; if (error) return 'An error has occurred: ' + error.message; console.log(data); return ( {data.results.map(user => ( <div key={user.id.value}> Name: {user.name.first} {user.name.last} <h2>Gender: {user.gender}</h2> <h2>Phone: {user.phone}</h2> <h2>Email: {user.email}</h2> </div> ))} </div>

Your App component and all of its descendants can now use React query as the QueryClient instance is provided to all components in the chain.

# Setting the contextSharing prop to true

If the error persists, try setting the contextSharing prop to true in your QueryClientProvider .

App.js
import React from 'react'; import { QueryClient, QueryClientProvider, useQuery, } from 'react-query'; const queryClient = new QueryClient(); export default function App() { // 👇️ Wrap with QueryClientProvider // and set contextSharing prop to true return ( <QueryClientProvider client={queryClient} contextSharing={true} <Users /> </QueryClientProvider>