Old data populates the form when editing an item #2732
Unanswered
boris-arkenaar
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In my app I have a CRUD going on with a list view and a form for creating a new item and editing an existing item.
The form consists of two steps (two separate pages) and I'm using a context to keep track of the data in the form.
In case we're editing an existing item I fetch that item using SWR and pass the result on to my context provider, which will populate the form.
Now, when you go through the following flow, I end up with stale data in the form:
This is because, on the second edit, stale data is present, which will be passed on to the context provider. SWR will revalidate, but when the new data arrives, the context is already satisfied with the stale data and won't update. It also shouldn't update, the user might have changed some form values already.
Solution would be to wait for the stale data to be updated before rendering the provider and the form. How to know when that's the case though? I thought
isValidating
would work here, but it doesn't. I guess because there's a brief moment where the stale data is present, but SWR is not revalidating yet, so isValidating would be false for a brief moment to start with.I came up with a solution that does work, where I wait for
isValidating
to become true first, then wait till it becomes false again. After that, I will render the provider and the form.With the following code:
This isn't very pretty. I'm wondering if there is a better approach to this problem? Or maybe a better approach to using a context in this situation?
Beta Was this translation helpful? Give feedback.
All reactions