T
TanStack7d ago
dependent-tan

Does the queryFn need to have a stable reference?

When providing useQuery with a queryFn, will the query fire each time it receives a new queryFn reference? E.g., when defining a function inline:
function useFoo() {
const queryFn = () => Promise.resolve("ok");
const q = useQuery({
queryKey: ['foo', 'bar'],
queryFn,
});
}
function useFoo() {
const queryFn = () => Promise.resolve("ok");
const q = useQuery({
queryKey: ['foo', 'bar'],
queryFn,
});
}
3 Replies
dependent-tan
dependent-tanOP7d ago
From local testing, it seems there's absolutely no effect from changing the function between renders, only the query keys have an effect. Is that the intended behavior?
ambitious-aqua
ambitious-aqua7d ago
Yes, the queryFn is only to be ran when queryKey or other things change. Not sure if this works (even if it does, it is a scuffed way of doing it, as it breaks other integrations like persist) you might be able to add the queryFn reference to the queryKey you could also call queryClient.invalidateQueries when you change your queryFn so they rerun the only time I know of that a query gets ran on a callback reference change is for the select option
rare-sapphire
rare-sapphire6d ago
no.

Did you find this page helpful?