How should I stop useMutation
hook from re-rendering my component?
#3644
Replies: 5 comments 6 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 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