Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
React-query hook imported into a component

Having the following hook:

import { useQuery } from 'react-query';
import axios from 'axios';

export const useGetData = () => {
  const query = 'getData';

  const { isLoading, isError, data, error, refetch, isSuccess } = useQuery(
    query,
    async () => {
      const { data } = await axios(
        'https://api.chucknorris.io/jokes/random'
      );
      return data;
    }
  );

  return {
    data,
    isError,
    isLoading,
    error,
    refetch,
    isSuccess,
  };
};

export default useGetData;

For the beginning, I want to import it into a component and check the values of data, isError and others.

Here is the component:

import React from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';
import useGetData from './components/use-get-data';
const queryClient = new QueryClient({});

function App() {
  const { data, isLoading } = useGetData();
  console.log('data: ', data);
  console.log('isLoading: ', isLoading);
  return (
    <QueryClientProvider client={queryClient}>
      <div className='container'></div>
    </QueryClientProvider>
  );
}

export default App;

It throws the following error:

Uncaught Error: No QueryClient set, use QueryClientProvider to set one
    at useQueryClient 

How can this be fixed?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your call to your hook need to be under the query client provider to access his context. So you need to put it in a component called as a child of QueryClientProvider

about 4 years ago · Juan Pablo Isaza Report

0

You should put the useGetData custom hook in the descendants component of the QueryClientProvider provider so that you can use useQuery hook.

Because useQuery hook use useQueryClient hook underly, see source code, and useQueryClient use useContext hook underly, source code:

export const useQueryClient = () => {
  const queryClient = React.useContext(
    getQueryClientContext(React.useContext(QueryClientSharingContext))
  )

  if (!queryClient) {
    throw new Error('No QueryClient set, use QueryClientProvider to set one')
  }

  return queryClient
}

queryClient context comes from QueryClientProvider so that useContext can get it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!