Suggestion: Recipe for auto save using RTK Query #3132
einarq
started this conversation in
Community Recipes
Replies: 2 comments 3 replies
-
@phryneas My current solution involves creating a custom hook that checks if changes were made, and then does manual invalidation once it unmounts, so that its no reloading while you are editing. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@markerikson |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to replace some custom loading and saving logic in my app with RTKQ.
This app is using auto-save, and its currently storing form state in redux (not recommended I know, but works fine for us in this case, and would be problematic to refactor right now).
The main issue I'm facing now is related to invalidation. When the user makes some changes, the form should automatically save after 1.5 seconds. And the save endpoint is set up to invalidate the query endpoint, so that a new fresh copy is retrieved whenever save finishes. But the user might still be making changes while save is happening, which results in lost changes once the new copy comes back due to the invalidation.
I'm wondering what the recommended approach is for this type of scenario?
Some thoughts:
Probably leaning towards option 1, but looking for some guidance here since it would obviously be better to leave the automatic invalidation in place.
Any thoughts on this? Perhaps there should be a recipe in the docs for this?
I found an npm package for a custom hook with React Query for this, with an option to merge the incoming data with the form data (if I read it correctly).
https://github.com/lukesmurray/react-query-autosync
Beta Was this translation helpful? Give feedback.
All reactions