T
TanStack16mo ago
rival-black

useQuery options changed per hook invocation

Hi there, I'm adoptiong react-query throughout my app and have a breif question regarding the architecture: Say I have a custom hook that wraps useQuery, and accepts one argument to override a useQuery option:
function useCustomQuery(refetchInterval: number) {
return useQuery({
queryKey: ['custom'],
refetchInterval
})
}
function useCustomQuery(refetchInterval: number) {
return useQuery({
queryKey: ['custom'],
refetchInterval
})
}
If I invoke useCustomQuery from N different components throughout my app, passing different values of refetchInterval, which value will it use? My assumption is that it would end up using the value from whichever component is rendered most recently, which would be pretty confusing. So you'd probably want to also wrap an internal useState to store the refetchInterval value, and then expose the setter in order to change it imperatively. Am I on the right track, or is there some other pattern at play here?
4 Replies
other-emerald
other-emerald16mo ago
This is per observer… so more refetches than you like
manual-pink
manual-pink16mo ago
@M00LTi is right. Every instance will have its own refetchInterval
rival-black
rival-blackOP16mo ago
interesting. so if I invoked useCustomQuery in one component with 1000ms, and in another with 300ms, the query would actually be fired on: 300, 600, 900, 1000, 1200, 1500, 1800, 2000, ...and so on. is that right? and if they were both invoked with 1000ms, the query would be fired twice every second (assuming no staleTime)?
manual-pink
manual-pink16mo ago
RefetchInterval guarantees one fetch in at least the interval time, and it should reset once a fetch happens. I never tried it out with multiple different intervals so please just try it out and report the findings 🙂

Did you find this page helpful?