Sub topic 1
What an optimistic update is and why it helps
Prerequisites
This chapter assumes you know:
- React function components and the
useStatehook. - How props flow down and how state is lifted up to a parent.
- Basic
async/awaitand Promises in JavaScript. - How to send data to a server with
fetchor a server function.
You do not need to know useOptimistic yet. Later chapters cover it.
The problem: waiting for the server
Most interfaces that change data follow the same steps. The user clicks a button, the app sends a request, and the interface updates only after the server replies.
That works, but it feels slow. On a good connection the delay is a few hundred milliseconds. On a poor one it can be seconds. During that time the screen shows the old state, and the user wonders whether the click registered.
Here is a typical "wait for the server" like button.
import { useState } from "react";
function LikeButton({ initialLiked, saveLiked }) {
const [liked, setLiked] = useState(initialLiked);
const [saving, setSaving] = useState(false);
async function handleClick() {
setSaving(true);
const next = !liked;
await saveLiked(next);
setLiked(next);
setSaving(false);
}
return (
<button onClick={handleClick} disabled={saving}>
{liked ? "Unlike" : "Like"}
</button>
);
}The label changes only after saveLiked resolves. Until then the button is disabled and shows stale text.
The optimistic approach
An optimistic update shows the expected result of an action right away, before the server confirms it. The app assumes the request will succeed. This is a bet, and most requests do succeed, so the bet usually pays off.
The flow looks like this:
- The user acts.
- The interface immediately shows the result the user expects.
- The request runs in the background.
- When the request finishes, the interface shows the real state from the server.
The user sees the change at the moment of the click. The network delay still exists, but the user no longer waits on it.
The trade-off
Optimistic updates trade certainty for speed. You show something that might not be true yet. That creates two obligations:
- If the request fails, the interface must go back to the true state. A "Liked" button that was never saved is a lie.
- If the failure matters, the user must be told. A silent rollback looks like a bug.
The table compares the two approaches.
| Row | Wait for the server | Optimistic update |
|---|---|---|
| Feedback after a click | After the response | Immediately |
| Accuracy of what is shown | Always confirmed | Expected, confirmed later |
| Code you write | Less | More: pending marks, rollback, errors |
| Failure experience | Nothing changed | Change appears, then disappears |
Where React 19 fits
Writing this by hand with useState is possible, but it is easy to get wrong. You have to store the temporary value, remember the previous one, and restore it on failure. You also have to handle two requests that overlap.
React 19 provides the useOptimistic hook for this job. You give it the real state. It gives you a temporary copy that you can change during an asynchronous action. When the action finishes, React drops the temporary copy and shows the real state again. Because the temporary value is never the source of truth, a failure leaves nothing to clean up.
Here is a preview of the same like button using the hook. Do not worry about every detail yet. The next chapters explain each part.
import { startTransition, useOptimistic } from "react";
function LikeButton({ liked, saveLiked }) {
const [optimisticLiked, setOptimisticLiked] = useOptimistic(liked);
function handleClick() {
startTransition(async () => {
setOptimisticLiked(!liked);
await saveLiked(!liked);
});
}
return (
<button onClick={handleClick}>
{optimisticLiked ? "Unlike" : "Like"}
</button>
);
}Two differences stand out. The liked value now comes from props, so the parent owns the real state. The button also never calls a setter after the request, because the parent updates liked and React swaps the optimistic value out.
Good candidates
Optimistic updates suit actions that:
- Succeed most of the time, such as likes, toggles, and sending a chat message.
- Are easy to undo visually.
- Do not cause side effects the user cannot take back.
Later chapters cover the cases where they are a poor fit.
Exercise
A user clicks "Like" and the request fails after three seconds. Name the two things an optimistic interface should do, and say why a plain "wait for the server" interface does not need them.
Show answer
It should return to the true state (not liked) and tell the user the like was not saved. A wait-for-the-server interface never showed the unconfirmed result, so there is nothing to undo and nothing misleading on screen. It only needs to report the error.
Exercise
In the useOptimistic like button, why is liked passed in as a prop instead of being stored with useState inside the component?
Show answer
The prop is the real, confirmed state, owned by the parent. useOptimistic builds the temporary value from it and drops that value when the action ends. The component then shows the real state again without extra code.