Sub topic 12
When an optimistic update is the wrong choice
This chapter assumes you know useOptimistic, Actions, and the automatic revert from earlier chapters. It also assumes you are comfortable with async/await and useState. It covers when to skip the hook.
The trade-off you are making
An optimistic update shows a result before the server confirms it. You gain speed. You pay with the risk that the screen shows something that never happens.
For a chat message or a like button, that cost is small. The rollback is visible, cheap, and easy to explain. For other operations, a wrong guess misleads the user in a way that matters. The question to ask is: "If this fails, what has the user already seen, and what might they have done because of it?"
Cases where you should not update optimistically
The action has real-world consequences
Payments, sending an email, placing an order, and deleting an account are hard or impossible to undo. If the screen says "Paid" and the charge then fails, the user may leave the page believing the bill is settled. Show the real state only.
The failure rate is high
The revert is a fallback, not a normal path. If a request often fails because of validation rules, permissions, or rate limits, users will see items appear and vanish. That feels worse than a short wait. Validate on the client first, or wait for the server.
The client cannot predict the result
A client can guess that a message was added. It cannot guess a server-calculated tax total, a generated invoice number, or whether a username is still free. Showing a guessed value that later changes is misleading.
Other people or systems react immediately
If a change notifies other users, triggers a webhook, or starts a workflow, the optimistic screen understates what has happened. Another user may see the change before the local user's request has even finished.
The request is already fast
If the server answers in about 100 ms, the optimistic state flashes and is replaced. You add complexity with no benefit. A simple pending indicator is enough.
Updates depend on each other
Several optimistic updates can overlap, as the previous chapter showed. If a later step needs the confirmed result of an earlier one, such as a server-assigned ID, the optimistic value is not enough to build on.
A decision guide
What to use instead
For these cases, show the pending state without pretending the result is final. useTransition gives you an isPending flag while an Action runs.
import { useState, useTransition } from "react";
function PayButton({ orderId, pay }) {
const [isPending, startTransition] = useTransition();
const [status, setStatus] = useState("unpaid");
const [error, setError] = useState(null);
function handleClick() {
setError(null);
startTransition(async () => {
try {
await pay(orderId);
setStatus("paid");
} catch {
setError("Payment failed. You have not been charged.");
}
});
}
return (
<>
<p>Status: {status}</p>
<button onClick={handleClick} disabled={isPending}>
{isPending ? "Processing..." : "Pay now"}
</button>
{error && <p role="alert">{error}</p>}
</>
);
}The status changes to "paid" only after pay resolves. The button is disabled while the request runs, which also prevents duplicate charges. For forms, useFormStatus gives the same pending flag to a child of the <form>.
A middle path
Sometimes you can be optimistic about part of the screen. You might add a "Processing" row to an order list straight away, but never show the order as complete until the server says so. The optimistic item then states what is true: the request is in flight. This is the pending-marker idea from earlier, applied with more caution.
Common mistakes
- Using
useOptimisticbecause it is available, not because the wait hurts. - Hiding the rollback. If an item disappears with no message, users cannot tell failure from a glitch.
- Showing optimistic values for fields the server computes.
- Leaving the submit control enabled for irreversible actions.
Exercise
A checkout page has a "Place order" button. The server checks stock, calculates tax, and charges the card. A teammate proposes using useOptimistic to show "Order confirmed" immediately. Give two reasons to reject this, and name what to use instead.
Show answer
The charge is hard to undo, and a false "Order confirmed" may lead the user to leave believing it succeeded. The tax total and stock check are decided by the server, so the client cannot predict the result, and failures such as out-of-stock are common enough to cause visible flip-flopping. Use useTransition (or useFormStatus) to disable the button and show a pending message, and update the status only after the server responds.
Exercise
A "Like" button gets a response in about 80 ms and fails rarely. Is useOptimistic the wrong choice? Explain.
Show answer
It is usually still reasonable, because a like is easy to undo and the client can predict the result. But at 80 ms the benefit is small. A plain pending state may be enough, so the decision depends on whether the delay is noticeable on slower networks.