Being optimistic in UI
Tags: ux, beginners, react, webdev
Published: 2019-09-05 17:37:46

---
title: Being optimistic in UI
published: true
description: Quick introduction to optimistic ui
tags: #ux #beginners #react #webdev
---

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/cyaubn441xuo32zt04k3.jpg)

Optimistic UI is not a new thing (games developers work with the idea of [client-side prediction](https://en.wikipedia.org/wiki/Client-side_prediction) for a long time), but the concept has been gaining a [lot of attention](https://www.apollographql.com/docs/react/features/optimistic-ui/) lately. 

The idea is simple. Based on a user action, an interface is updated, even though a request to the backend may still be pending. In the end, an optimistic UI is nothing more than a way to manage [perceived performance](https://blog.teamtreehouse.com/perceived-performance) and avoid loading states.

The workflow can be straightforward in React.

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

Eliminating small lags from the UI interaction seems to create a faster and much more responsive perceived user experience. However, there are some drawbacks involving optimistic UI.

First, optimistic UI is not a one-size-fits-all solution. One crucial point is that if there is a failure in the server ([false positive](https://en.wikipedia.org/wiki/False_positives_and_false_negatives)), the UI update should be reverted graciously, and this is difficult to achieve if the action triggered by the user plays a role in the application's routing.

Plus, since there is the risk of false positives, optimistic UI doesn't seem a good idea for checking for an air flight or a cash transfer experience. Imagine saying to the user that this cash transfer was reverted.

On the other hand, for actions that are not so important (like a post or posting a quick message, for example), optimistic UI seems OK.

The second point is that optimistic UI works better with actions that generate boolean values (true or false, yes or no), things that, in case of false positive, can be easily reverted.

In my experience, the most critical point of the technique when something didn't go well is making it noticeable. Handling UI errors is very important. The interval between the action and the error message couldn't be [more than two seconds](https://www.nngroup.com/articles/response-times-3-important-limits/).

I created a small app that shows how optimistic UI can be implemented. As soon as you answer one of the questions, the UI is updated, even though the request is still pending. The app can be seen [here](https://optimistic-updates.netlify.com/), and the code is [here](https://github.com/tiagodcosta/optimistic-updates).

_This post is part of a series about UX and UI engineering._

Photo by [Brooke Cagle](https://unsplash.com/search/photos/laptop-happy?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/@brookecagle?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)