UseQuery with "now" in paramaters and refetchInterval
I have a useQuery call where the promise callback parameter requires a current timestamp value that we provide with Luxon's DateTime.utc().toISO(). As this value changes every millisecond the query also fires off as fast as it can go. To circumvent this we need to use a useEffect with a setInterval in it that sets a state. I have the feeling there could be a better solution around. As the param changes the query fires so adding a refetchInterval of a minute to the query adds more oil to the fire. Is there a way to set the timestamp param value when the refetch is fired without the param changing causing a new fetch itself? Or is controlling the param update the only way to go?
4 Replies
wise-white•11mo ago
What if you just don't include the parameter in the key? The rule is to have all the parameters as explaind here: https://tanstack.com/query/latest/docs/eslint/exhaustive-deps
But you don't have to do this.
TanStack | High Quality Open-Source Software for Web Developers
Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.

adverse-sapphireOP•11mo ago
We use TRPC so I'm afraid the key part is abstracted away somehow
rare-sapphire•11mo ago
the exhaustive deps rule definitely won't flag a use of an imported method - it's to avoid stale closures. If you put the current timestamp in the key, you will get new cache entries with every request. This isn't what you want
if trpc abstracts the queryFn away, add the timestamp on the
fetch
layer
but if you call your own trpc server WHY do you need the timestamp to be sent ?
I mean you can centrally add it as a header, tooadverse-sapphireOP•11mo ago
Thanks @TkDodo 🔮 for the insights. Very good point you are making. I think I will refactor my procedure in a way that the timestamp is set there so a refetch would use whatever the current time is and it doesn't have to be part of thr key. I think I avoided it so far because I was reusing the procedure, with different params but I can work around that as well.