T
TanStack•14mo ago
metropolitan-bronze

How to handle a function dependency of the query/mutation function?

Given a query or mutation where the query/mutation function depends itself on another function, will the query/mutation always run with the most up-to-date function? Should this be managed somehow? E.g.,
const myFunc = useMyFunc();
const mutation = useMutation({
mutationFn: async () => {
const myResult = await myFunc(); // <-- Will this always be up to date?
const res = fetch(myResult.url);

// ...

return res.json()
},
});
const myFunc = useMyFunc();
const mutation = useMutation({
mutationFn: async () => {
const myResult = await myFunc(); // <-- Will this always be up to date?
const res = fetch(myResult.url);

// ...

return res.json()
},
});
3 Replies
conscious-sapphire
conscious-sapphire•14mo ago
Hi, the short answer is yes. When mutationFn will be called it will call myFunc which will point to the actual function returned from the useMyFunc hook. So if useMyFunc doesn't do anything stupid, then you are probably good.
metropolitan-bronze
metropolitan-bronzeOP•14mo ago
Thanks!
conscious-sapphire
conscious-sapphire•14mo ago
no problem 🙂

Did you find this page helpful?