queryClient.setQueryData dirty state #1626
-
I am looking for a clean way to know when my queryClient data is dirty. Maybe the 'dirty' terminology is not immediately clear, so I try to clarify this: Whe When I move away from the form I want to warn the user about unsaved changes. To define 'unsaved' changes I am looking for some kind of dirty state. Am I just using wrong terminology or am I looking for something that is not possible with react-query? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I don't think that you should be using the queryClient as a "local" state manager. if you are using background updates or other forms of re-fetches, your data will be overwritten by the server state. the idea is that you and the queryClient don't own the state - it's just a view of what is on the server, which is as up-to-date as you like it / make it. The only way I am ever writing to the query cache is:
I would suggest that you keep the "edits" that the user makes separate from the queryClient. Can be in local react state, or another state manager like zustand or even redux. Your local data is then dirty if it's not I have written about this concept here: https://tkdodo.eu/blog/practical-react-query |
Beta Was this translation helpful? Give feedback.
-
Thank you for sharing your thoughts. I also experimented with mutating the queryClient cache, but works more against then with react-queries defaults. For completeness: I now manage 'dirty' state in both parent (component) state and In Zustand. |
Beta Was this translation helpful? Give feedback.
-
@TkDodo I don't understand why should we limit ourselves to not use the queryClient cache as local state manager. I think for many scenarios it works perfectly well. Especially when react-query does provide ways to enable or disable background updates, doesn't it make sense to stop background updates and refetches when user is editing and continue after its pushed to the server. As an end user, I also would expect the same, not to be magically have new entries or be removed while I am editing ! |
Beta Was this translation helpful? Give feedback.
I don't think that you should be using the queryClient as a "local" state manager. if you are using background updates or other forms of re-fetches, your data will be overwritten by the server state.
the idea is that you and the queryClient don't own the state - it's just a view of what is on the server, which is as up-to-date as you like it / make it. The only way I am ever writing to the query cache is:
I would suggest that you keep the "edits" that the user makes separate from the queryClient. Can be in local react state, or another state manager like zustand or even redux.
Your local data…