Support for optimistic updates
See original GitHub issueI’m trying to figure out how to implement optimistic updates with createAsyncThunk
and I came up with something like this:
export const updateTodo = createAsyncThunk<
Todo,
{
id: Todo["id"];
changes: Partial<Todo>;
}
>("todos/update", async (args, { dispatch, getState, requestId }) => {
const { id, changes } = args;
const previousTodo = (getState().todos as TodosState).entities[id] as Todo;
const begin = () => {
const todo: Todo = {
...previousTodo,
...changes,
updatedAt: new Date().toISOString()
};
dispatch(updateTodo.fulfilled(todo, requestId, args));
};
const commit = () => todosApi.update(id, changes);
const rollback = () => {
dispatch(updateTodo.fulfilled(previousTodo, requestId, args));
};
begin();
try {
return commit();
} catch (error) {
rollback();
throw error;
}
});
I’d like to share this piece of code because I believe that it could ba a good starting point for something like a generic helper for such actions.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Optimistic mutation results - Apollo GraphQL Docs
Optimistic mutation results · Update your UI before your server responds · The optimisticResponse option · Optimistic mutation lifecycle · Example: Adding a...
Read more >Optimistic Updates - Query
Optimistic Updates. When you optimistically update your state before performing a mutation, there is a chance that the mutation will fail.
Read more >Optimistic updates with concurrency control | by Alan Torres
Optimistic updates is a common pattern to improve the user experience by allowing the UI to behave as if a request to your...
Read more >The Magic of Optimistic UI Updates with React and Redux
An optimistic UI update is an update in the user interface showing the final result even before the change requested to the server...
Read more >what is `optimistic updates` in front-end development
In an optimistic update the UI behaves as though a change was successfully completed before receiving confirmation from the server that it ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the suggestion! I believe RTK already supports optimistic updates. This is how you could implement the exact same feature without any changes to RTK:
This has a lot of benefits over the solution you proposed, including:
@AndrewCraswell are patches now exposed? I want to apply optimistic UI in the way you’ve suggested but I can see now documentation about it. @arctouch-danielbastos’s comment is very helpful but would lead to more duplicated code in my case