DEV Community

Being optimistic in UI

Tiago Costa on September 05, 2019

Optimistic UI is not a new thing (games developers work with the idea of client-side prediction for a long time), but the concept has been gainin...
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
 
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.

Collapse
 
draer profile image
Michał Murawski

But... But...

Collapse
 
sagar profile image
Sagar • Edited

Great article. Appreciated 👏👏👏

Collapse
 
tia profile image
tia

Thank you for sharing. This is actually dated back to the terminal era, where local echo was invented because remote responses couldn't catch up with the typing speed of the user.

Collapse
 
marcoslooten profile image
Marco Slooten

Definitely an interesting read (and topic).

"In the end, an optimistic UI is nothing more than a way to manage perceived performance and avoid loading states."

Yeah I totally agree. From a user's perspective, I guess perceived performance is the only performance that really matters. As long as it can fail gracefully without side-effects. I'm implementing state machines on everything that does optimistic updates, I feel it helps to keep everything in check.

Collapse
 
dmcorrales profile image
Daniel Corrales

Great!