T
TanStack•17mo ago
unwilling-turquoise

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
rival-black
rival-black•17mo 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.
unwilling-turquoise
unwilling-turquoiseOP•17mo ago
Thanks!
rival-black
rival-black•17mo ago
no problem 🙂

Did you find this page helpful?