useMutation's reset() method runs on every other call from inside then() callback #3326
-
Hello everyone I wanted to try and reset a mutation to its original state after success. I added the reset() function inside the then() callback of the mutation trigger's returned promise.
However, the hook's state is not being reset after every click, instead it's being reset after every alternate click. I am new to RTK-query and unable to figure this out. Can someone help me find out the reason for this behaviour? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem is that you will only get the
You could for example work around it like this: const [createPost, { reset }] = useCreatePostMutation();
const resetRef = useRef(reset)
resetRef.current = reset
const handleClick = () => {
createPost().then(() => resetRef.current());
}; |
Beta Was this translation helpful? Give feedback.
The problem is that you will only get the
reset
method for the currently running request in the next render.createPost().then(() => reset());
reset
here is a reference to thereset
function that existed beforecreatePost
was started.You could for example work around it like this: