How should I stop useMutation hook from re-rendering my component?
#3644
Replies: 7 comments 8 replies
-
|
the component re-renders because If your component is crashing because of a re-render, then I'm afraid there is something wrong with your component. |
Beta Was this translation helpful? Give feedback.
-
|
because sometimes when this function is passed as a context variable to a local module, it re-renders the entire module, resetting all its states. which may not be expected always. |
Beta Was this translation helpful? Give feedback.
-
|
@TkDodo I am bumping into a similar problem, I have a component that reads URL params and passes them to the useQuery hook, in that component I also have a useMutation hook, that deletes the object. |
Beta Was this translation helpful? Give feedback.
-
|
The problem with re-renders is that, any input field within the component that was in focus will loose focus. How to override this? useMutation is causing a re-render. Is there a way to overcome this? any hack or workaround? |
Beta Was this translation helpful? Give feedback.
-
|
I solved it with optimistic mutation. |
Beta Was this translation helpful? Give feedback.
-
|
i'm using mutateAsync inside a loop and it trigger a warning because of the rerender |
Beta Was this translation helpful? Give feedback.
-
|
Sorry for the late answer to this, I came across the question today and had an epiphany over this. You don't need to use tanstack/query for every API calls. I was starting write a contraption to somehow box If you don't need to rerender a component just don't use import { changeMyStuff } from './api-functions';
export function useChangeMyStuff() {
const queryClient = useQueryClient();
// since I want to return an async function I need to resort some ES5 pro hackery
// to curry the queryClient, but you don't need to complicate it.
return myStuff.bind(null, queryClient);
}
async function myStuff(queryClient, params) {
// in case you need the result
const result = await changeMyStuff(params);
// This totally works! This is entirely optional, I just show that this is possible
await queryClient.invalidateQueries( { queryKey: ['myStuff'] });
return result;
}And if you still need to usage the result somewhere you can use something like overmindJS, or redux, or React context. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
I am using useMutation hook. Every time when I call it, the component is re-rendering, all my data are getting lost and I am getting into trouble. How could I prevent useMutation do not re-render my component?
Here is what I am doing:
As first I am importing useMutation hook like this:
import { useMutation } from 'react-query'
Then I am doing destructing like this and I am passing the function that I want to call (in my case updateDetails function which am also importing in the top of the code:
const { mutate } = useMutation(updateDetails)
Then I am calling the mutation inside submit function like this:
const submitHandler = (e) => {
e.preventDefault()
const response = mutate(updateDetails(name, index))
console.log(response)
}
where,
The problem is that as first the index property is getting the right number, then the component re-renders, and the index is getting undefined and the function crashes.
How should I solve this issue?
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions