Sub topic 11
How repeated submissions are queued
This chapter assumes you know useActionState basics: the action receives the previous state and FormData, and returns the next state. You should also be comfortable with async functions and promises.
The problem: a second submit while the first is running
An action can take a while, for example when it awaits a network call. A user may click the submit button twice, or dispatch from several places. If two actions ran at once, each could start from the same old state, and the slower one could overwrite the result of the faster one.
useActionState avoids this by running actions one at a time, in the order they were dispatched.
How the queue works
When you dispatch while an earlier action is still pending, React does not start the new action. It puts the call in a queue. When the running action finishes, React takes the next call from the queue. It passes that call the state the previous action returned.
So prevState is always the result of the most recent completed action, never a stale value. This is why the array example from earlier chapters is safe: each added item builds on the previous array.
Seeing it in action
This example makes every action wait one second. Click the button quickly several times and watch the log.
import { startTransition, useActionState } from "react";
async function slowIncrement(prevCount, amount) {
console.log("start from", prevCount);
await new Promise((resolve) => setTimeout(resolve, 1000));
const next = prevCount + amount;
console.log("finish with", next);
return next;
}
export function SlowCounter() {
const [count, dispatch, isPending] = useActionState(slowIncrement, 0);
return (
<button onClick={() => startTransition(() => dispatch(1))}>
Count: {count} {isPending && "(working...)"}
</button>
);
}After three quick clicks, the console shows each action starting only after the previous one finished: start from 0, finish with 1, start from 1, finish with 2, start from 2, finish with 3. The count never skips or repeats a value.
isPending stays true until the whole queue is empty, not just until the first action ends.
What queueing does not do
- It does not cancel or merge calls. Three clicks mean three actions, so the server receives three requests.
- It does not stop duplicates. Disabling the button with
isPendingis the usual way to prevent accidental double submits. - It does not skip a failed action. If you want later actions to proceed, return an error state from the action instead of throwing.
Choosing what to do about it
| Situation | Approach |
|---|---|
| Actions that depend on earlier results, such as appending to a list | Rely on the queue |
| Form where a repeat submit is a mistake | Disable the button with `isPending` |
| Rapid events where only the last value matters | Debounce before dispatching, or use `useState` |
Exercise
A user clicks a button three times quickly. It dispatches an action that awaits a 1 second delay and then returns prevCount + 1, starting at 0. What prevCount does the third action receive, how long does the whole sequence take, and how many requests would a fetch inside the action send?
Show answer
The third action receives 2, the result of the second action. The sequence takes about 3 seconds, because the actions run one after another. A fetch inside the action sends 3 requests, since queued actions are not merged or cancelled.
Exercise
Why is it a good idea to return an error object from an action rather than throw when you want queued submissions to keep working?
Show answer
A thrown error stops the queue and goes to the nearest error boundary, so the pending later actions do not run normally. Returning an error object as state lets the queue continue, and the next action receives that state as prevState.