#useQuery always refetching

18 messages · Page 1 of 1 (latest)

tame python
#

Hi 👋 I'm new to using useQuery and I think I already follow the instructions from the documentation, so here is the snippet of my code,

const { data, isLoading, error } = useQuery({ queryKey: ["financialSankey", ticker], queryFn: () => fetchReport(ticker), staleTime: 1000 * 60 * 60, // 1 hour refetchInterval: false, refetchOnMount: false, refetchOnWindowFocus: false, refetchOnReconnect: false, });

as you can see I already try to set the staleTime and also set all the refetch options to false. But when I check on the network tab, it always refetching. Is there something wrong with my understanding?

left fog
tame python
#

Hi @left fog my QueryClient already made the same as the stable-query-client example

`const TanstackProvider = ({ children }: { children: React.ReactNode }) => {
const [queryClient] = useState(() => new QueryClient());
return (
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
};

export default TanstackProvider;`

am I missing something?

left fog
#

looks good, can you reproduce it in codesandbox ?

tame python
#

Sorry, but I am new to these things so I am afraid I don't know how to reproduce it. But maybe will it be a problem if TanstackProvider is being used in the RootLayout component inside the layout.tsx file?

#

also, should I set the gcTime the same as staleTime?

stuck narwhal
#

What is ticker? If that’s a changing value like on a setInterval or something then your queryKey would change in turn calling the queryFn again

tame python
#

@stuck narwhal ticker is part of the url so the url basically looks like this: http://localhost:3000/idx/[ticker]

#

the fetchReport(ticker) will try to get the data from db (I'm using supabase) based on which ticker is being access

stuck narwhal
#

Okay I was just taking a wild guess. Like Dominik said next step would be a reproduction then

tame python
#

Thanks @stuck narwhal but actually is the way I use the useQuery correct? should I set the gcTime too? What I want is to only refetch the data after 1 hour

stuck narwhal
#

You just need staleTime for that

tame python
#

so no need to set all those 'refetch' option then?

stuck narwhal
#

Not necessarily. We disabled things like refetchOnWindowFocus on our queryClient because tabbing away and back in was an unwanted refetch for us

tame python
#

So if it hasn't been disabled in the queryClient, then I still need to set it as above?

stuck narwhal
#

Setting options on the queryClient means it’ll be the default for all your queries

#

useQuery options has higher priority, but if you’re defining the same options over and over then put it on the queryClient

tame python