12

I feel there are hints in the documentation about setting the default options for useQuery, but I cannot figure out exactly how. For example, the QueryClient takes a defaultOptions constructor but then it's not clear how to attach that to useQuery. Maybe it is not possible?

3
  • Do you want to change these options for specific queries or just all queries in general? Commented Oct 26, 2022 at 6:14
  • yeah I wish to change the global default behaviour of useQuery Commented Oct 27, 2022 at 9:33
  • Then doing so in the queryClient's defaultOptions is the way to go. These defaults will be used by all queries using that particular query client. Commented Oct 27, 2022 at 18:18

1 Answer 1

23

defaultOptions on the QueryClient will be applied to all queries, unless they have been specifically overwritten:

const queryClient = new QueryClient({
  defaultOptions: { queries: { staleTime: 5000 } },
})

if you want to set defaults for specific queries, you can do that with:

queryClient.setQueryDefaults(['todos'], { staleTime: 3000 })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.