Replies: 2 comments
-
no. For mutations, the mindset is: If you don't want it to execute, don't invoke |
Beta Was this translation helpful? Give feedback.
-
While it's true that if I don't want a mutation to execute, I simply shouldn't call .mutate, there are scenarios where a "cache mutation" feature could be beneficial. For example, if a user action, like clicking a button, triggers a mutation, it might be redundant to send the same data if nothing has changed. Implementing a check to avoid unnecessary mutations can improve performance and prevent redundant API calls, especially in cases where user interaction is mandatory. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was wondering if it's possible to prevent a mutation from occurring if the state value has not changed, especially in situations where pressing a button that holds a state value causes hundreds of data changes on the server.
So, I created a utility function called
compareDeepEqual
for deep comparison and a custom hook called useCachedMutate usinguseState
.- compareDeepEqaul
- How to use
I believe that by not executing the mutation at all when the state value is the same as before, it can save a lot of resources.
This is what I have created as an experiment. Does Tanstack Query have such a built-in feature?
Or is there a better way to achieve this?
If not, I think it would be beneficial to have such a mutation hook. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions