DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Fourteen useActionState, thirteen useTransition, zero useFormStatus, and 14 different ways of saying "working"

Every app that talks to a server has to answer the same small question in a hundred places: what does the screen do between the click and the answer. Nakodo has 62 Server Actions, so I went looking for how many of those hundred places exist and whether they agree with each other.

Across 616 source files:

count files
useActionState 14 11
useTransition 13 10
useOptimistic 3 3
useFormStatus 0 0
<form action={...}> 14 11
onSubmit= 2 2
disabled={...pending...} 55
pending ? "..." : ... 29
aria-busy 2 2
role="status" 16 12
animate-spin 3 3

62 actions, 27 places where a person waits. The rest are things like a toggle flipping or a row being removed, where the action is fired and the UI has already moved on.

The hook we never used

useFormStatus is the React hook built for exactly this, and it is used nowhere. Not from an opinion about it: it reports the status of the nearest parent <form>, so it only works inside a child component. Using it means extracting a <SubmitButton> out of every form purely so the hook has somewhere to live.

useActionState hands you the same flag where the button already is:

const [state, action, pending] = useActionState<AuthState, FormData>(
  mode === "signin" ? signIn : signUp,
  { ok: false },
);
...
<Button type="submit" size="lg" className="w-full" disabled={pending || !human}>
  {pending ? "One moment..." : !human ? "Checking you're not a bot..." : mode === "signin" ? "Sign in" : "Create account"}
</Button>
Enter fullscreen mode Exit fullscreen mode

One destructure, no extra component, and the button can read other state in the same expression. That last part is the real reason: the sign-in button has three states, not two, because it is also waiting for a bot check. A useFormStatus button would have needed that passed in as a prop anyway.

You can watch that exact ternary on our sign-in page: the button says "Checking you're not a bot..." for a moment before it says "Sign in".

14 ways to say the same thing

The 29 label sites use 14 distinct strings:

8  Sending...
4  Saving...
3  One moment...
2  Deleting...
1  Writing...   1  Working...   1  Stopping...   1  Starting...
1  Reopening...  1  Opening Stripe...  1  Looking...
1  Changing...   1  Agreeing...
1  Reading the site, 10 to 40 seconds...
Enter fullscreen mode Exit fullscreen mode

Every one of them is a present participle and an ellipsis, and every ellipsis is three full stops. The character … appears zero times in the codebase, which is consistent with a site whose visible text has 97 distinct code points and no typographic punctuation, and consistent by accident rather than by rule, because nothing enforces it.

The convention is unwritten but real, and so is its drift. "One moment..." and "Working..." and "Starting..." are three answers to the same design question, written months apart. An earlier count found 17 words for 8 database statuses; this is the same failure in the verb tense.

The outlier is the one I would keep. "Reading the site, 10 to 40 seconds..." is on the button that re-crawls a brand's website, which genuinely takes that long, and a label that quotes a range is worth more than any spinner. Everything else in this list takes under a second and the label is mostly ceremony.

Disabled is the whole design system

55 of the bindings are disabled, and the shared affordance is one line of the button's base class:

disabled:pointer-events-none disabled:opacity-50
Enter fullscreen mode Exit fullscreen mode

So a busy button is a 50 per cent opaque button with a different word in it. There is no <PendingButton>, no loading prop, and exactly one shared form component that bakes the pending label in:

<Button type="submit" disabled={pending || disabled}>
  {pending ? "Saving..." : "Save"}
</Button>
Enter fullscreen mode Exit fullscreen mode

That covers the panel forms. The other 28 sites each wrote their own, which is how you end up with 14 strings.

Three animate-spin in the whole app, two of them the same icon on the two editors where a model is writing something, and the third inside the toast component. For work that takes a second, a word beats a spinner. For work that takes thirty, the two editors dim the panel they are rewriting:

<div className={cn("min-w-0 space-y-6 transition-opacity", suggesting && "opacity-60")} aria-busy={suggesting}>
Enter fullscreen mode Exit fullscreen mode

Those are also the only two aria-busy attributes on the site.

Where optimism is allowed

Three useOptimistic calls, and what they have in common is that being wrong is cheap.

const [checked, setChecked] = useOptimistic(on);
const [pending, start] = useTransition();
...
onCheckedChange={(next) =>
  start(async () => {
    setChecked(next);
    notify(await save(next));
  })
}
Enter fullscreen mode Exit fullscreen mode

That is an email notification switch. If the save fails, the switch snaps back and a toast says so, and nothing has happened in the world. The second is a list where rows can be set aside in bulk, with a reducer that patches the ids in place. The third is an admin toggle.

Nothing that sends an email, starts a search or charges a card is optimistic. The rule I would write down, having now read all three, is: optimistic updates are for state the user can see is wrong and can set back themselves. Everything else waits and says so.

What the census says to fix

  1. A pending prop on the button. One place to decide what busy looks like, which is where the aria would go too.
  2. Two aria-busy is not enough. A disabled button with a new label is a weak signal for a screen reader: the name changes under a control that has just lost its pointer affordance, and nothing announces it. There are 16 role="status" regions in the app already, which is the right machinery pointed at the wrong events.
  3. Pick the verbs. Fourteen strings for one idea is the kind of thing a reviewer never catches, because each one is correct on its own screen.

What I would not change is the plain-words approach. The app has almost no spinners, and the one label that tells you how long it will take is the best thing in the list. That is cheaper than any animation and it is the only form of progress indication that is honest about an operation nobody can measure.

If you want to see the two-state version without an account, the sign-in page is a useActionState form, and viewing its source shows the limit of this whole approach:

<form class="space-y-5" action="javascript:throw new Error('React form unexpectedly submitted.')">
Enter fullscreen mode Exit fullscreen mode

The action we pass is a closure rather than a Server Action reference, so Next cannot put a URL in the attribute and the form does not submit at all without JavaScript. The pending state is the least of what is missing there. Our pricing page and how it works are the other side of the same app: no forms, no waiting, nothing to say.

Top comments (0)