Sub topic 8
Marking items as pending
This chapter assumes you know useState, props, and async/await. It also assumes you have read the earlier chapters on the useOptimistic signature, the update function, and form actions.
Why mark an item as pending
An optimistic item looks exactly like a real one unless you make it look different. That is a problem, because the server has not confirmed the item yet. It may still fail and disappear. A small visual cue tells the user the item is provisional. It can be faded text, a "Sending..." label, or a disabled button.
React does not track this for you. useOptimistic returns a value, and it has no idea which parts of that value are optimistic. You add that information yourself, inside the update function.
Add a flag in the update function
The update function builds the optimistic list from the current list and the new input. This is the place to attach a marker such as sending: true. Real messages from the server do not carry the flag, so only the optimistic entries have it.
import { useOptimistic } from "react";
function Thread({ messages, sendMessage }) {
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
messages,
(currentMessages, text) => [
...currentMessages,
{ id: crypto.randomUUID(), text, sending: true },
]
);
async function formAction(formData) {
addOptimisticMessage(formData.get("message"));
await sendMessage(formData);
}
return (
<>
<ul>
{optimisticMessages.map((m) => (
<li key={m.id} aria-busy={m.sending ? "true" : undefined}>
{m.text}
{m.sending && <small> (Sending...)</small>}
</li>
))}
</ul>
<form action={formAction}>
<input type="text" name="message" />
<button type="submit">Send</button>
</form>
</>
);
}The render code checks m.sending. For a confirmed message the property is undefined, so the label does not render.
Why the flag goes away on its own
You never write code that clears sending. When the Action finishes, React discards the optimistic value and renders the real messages prop again. The parent has by then updated messages with the server's version of the new message. That version has no sending flag, so the "Sending..." label disappears in the same render.
The lifecycle of one item looks like this:
This is why the flag must live only in the update function. If you copied it into data that the parent stores, the label would stay forever.
Style the pending state
A label is one option. Many interfaces fade the item instead. You can use the same flag to pick a CSS class.
<li
key={m.id}
style={{ opacity: m.sending ? 0.5 : 1 }}
aria-busy={m.sending ? "true" : undefined}
>
{m.text}
</li>The aria-busy attribute tells assistive technology that the item is still changing. Use it with a visible cue, not in place of one.
Restrict actions on pending items
A pending item has no real server record yet. Actions that depend on that record, such as delete or edit, can fail or target the wrong thing. Disable them while the flag is set.
<button disabled={m.sending} onClick={() => onDelete(m.id)}>
Delete
</button>Here the id is a temporary one made by crypto.randomUUID(). Sending it to the server would reference a record that does not exist.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Setting `sending: true` in the parent's real state | The label never goes away | Add the flag only in the update function |
| Mutating `currentMessages` with `push` | Unpredictable renders | Return a new array with spread or `map` |
| Showing the flag with `m.sending === false` for confirmed items | Confirmed items are treated as pending | Check for truthiness: `m.sending &&` |
| Letting users delete a pending item | Request uses a temporary ID | Disable actions while `sending` is set |
Exercise
A teammate adds sending: true inside the update function, but the "Sending..." label stays on screen after the server responds. The parent stores the new message by copying the optimistic item it received. What is the likely cause, and how do you fix it?
Show answer
The parent is saving the optimistic item, including its sending: true flag, into the real messages state. When React switches back to the real state, the flag is still there. Fix it by having the parent store the server's response (or build a new object without sending) so that confirmed messages never carry the flag.
Exercise
Change the delete button in a message list so a message cannot be deleted while it is pending. Which property do you check, and where does that property come from?
Show answer
Check m.sending and pass it to disabled, as in disabled={m.sending}. The property comes from the update function passed to useOptimistic, which adds sending: true only to optimistic items.