execute code after mutate
How do I wait for the mutate to done running before proceed to the code below?
because when I tried to getQueryData, the data is outdated, most probably at that point mutate is not done yet. i know i can use the onSuccess event on the useMutation hook, but i think this is not the useMutation hook responsibility to handle this. I have also tried useMutationAsync, it works, but i'm not sure if i'm suppose to do that because i saw the blog discourage to do that.

4 Replies
rival-black•3y ago
This is a side effect that belongs in an onSuccess callback in my opinion
like-goldOP•3y ago
hmm, because this button suppose to navigate to other page, its just that before navigate, check whether the private channel has already been created, if not call API to create it. so regardless the room is already created before, in the end it will still navigate user to the other page
so i think its the responsibility of the button? instead of useMutation hook responsibility, its not mandatory to navigate to other page after calling the API, but in that page, it require to navigate, so i think its the button responsibility
rival-black•3y ago
Oh I see what you mean, yeah. If you don't need to perform the mutation then you can navigate from the button click callback, yeah. If you do need to navigate after performing the mutation then I think that belongs in onSuccess
national-gold•3y ago
I think I'd still put it in the mutation. Your mutate function can decide whether it needs to trigger the API call to actually create, either by passing friend or retrieving it from elsewhere. If it exists, the onMutate could return early, else trigger the request to create.
Then you can use onSuccess/onSettled to refresh the cache or return the data you want from the mutation. Which I think is what you're looking to do, the button then gets to redirect knowing the channelId exists one way or the other.