Mutating same key during ongoing async mutation and optimistic state (two concurrent mutations) #2743
Unanswered
petrnovikov
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
-
I'm building a to-do list tool and trying to enable the following behavior when editing tasks:
1. Pressing "Enter" creates a new task below the current one and focuses it
2. User immediately can start typing text in the new task
Here's an example of how this works in Notion:
When I implement this functionality with optimistic updates the first part works as intended.
But, if the asynchronous fetcher function of the first mutate (create new task) is still running when I start typing in the new task (and hence calling the second mutate function) the state immediately rolls back to where it was before the new task was created.
Here's an example:
This problem doesn't occur, if the first async function finishes running before I start typing, it only occurs if the first mutate function is still running when the second mutate is called and the state is optimistic.
Below is simplified code demonstrating my approach (throttled async functions while typing, task ordering and focusing purposefully omitted for simplicity, but are implemented in the full code).
Here's a diagram explaining my issue:
This might be somewhat related to #735 and #922 but both are pretty old and relate to swr 1.0,
Beta Was this translation helpful? Give feedback.
All reactions