DEV Community

Discussion on: Being optimistic in UI

Collapse
 
dandy profile image
dandy • Edited

Capture a user action (click a button, for example).
Update the local state, which updates UI.
Send the request.
Get a response.
If negative, rollback update the local state (step 2).
If positive, do nothing or confirm step 2.

Can be simplified if you send ajax request to server first, and then, based on the result, update the local state. Then you dont need to worry about local state rollbacks.

Collapse
 
draer profile image
Michał Murawski

But... But...

Collapse
 
redbar0n profile image
Magne

But then if the ajax request fails, you've lost user data.

Rather than rollbacks, I'd use an automatic retry-policy. Then, if that fails notify the user and let him do a manual retry. Should probably have a 'failed operations inbox' for those kinds of notifications.