Other ways to loop .mutate and update state when onSuccess?
I need to loop a function that contains a .mutate and onSuccess it updates my state with values from the return data and data from the function parameters. I know that .mutate only fires a callback once, but what other ways can I go with? I don't think I can use useMutation, because it then has to be inside my function and a hook can't be called in a function in react.
3 Replies
compatible-crimson•3y ago
This probably has what you're looking for: https://tanstack.com/query/latest/docs/react/guides/mutations#consecutive-mutations
Mutations | TanStack Query Docs
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. For this purpose, TanStack Query exports a useMutation hook.
Here's an example of a mutation that adds a new todo to the server:
rival-blackOP•3y ago
Thanks for the link, but I need onSuccess to execute my set state each time, but since onSuccess executes once in mutate it only updates my state with only the last data. The docs tell me to use useMutation for looping onSuccess, but I need to execute the mutation in a function with different parameters.
compatible-crimson•3y ago
Okay, it sounded like the
onSuccess on the useMutation, not the mutate, was gonna give you what you wanted
The comment saying // Will be called 3 times