React: `action()` and `router.visit()` never resolve when awaited in a transition

Chiusa
#643 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@cpojer ci sta già lavorando.

Dal 30/10/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

React: action() and router.visit() never resolve when awaited in a transition

What happens

Awaiting action() or router.visit() inside a React transition never resolves: the transition's isPending stays true and code after the await never runs. It happens with startTransition(async () => …), a <form action={fn}> function and useActionState, which React all runs as transitions.

// pages/repro.server.ts
import { defineHandler, type InferProps } from 'void';

export type Props = InferProps<typeof loader>;
export const loader = defineHandler(async () => ({ loadedAt: Date.now() }));
export const actions = {
  ping: defineHandler(async () => {}),
};

// pages/repro.tsx
import { action } from '@void/react';
import { useTransition } from 'react';
import type { Props } from './repro.server';

export default function Repro({ loadedAt }: Props) {
  const [isPending, startTransition] = useTransition();

  function save() {
    startTransition(async () => {
      await action('/repro?ping');
      console.log('saved'); // never runs
    });
  }

  return (
    <button onClick={save} disabled={isPending}>
      {isPending ? 'Saving…' : `Save (loaded at ${loadedAt})`}
    </button>
  );
}

Clicking Save leaves the button on "Saving…" for good. The same await action() called from a plain onClick, outside a transition, resolves.

Versions: seen with void 1.3.0, @void/react 1.3.0 and react 19.3.1. The code involved is unchanged in 1.4.0.

Why

It's a circular wait. Line numbers are from the built 1.4.0 packages:

  1. action() calls submitAction(), which awaits router.visit() (void/dist/pages/client.mjs:1198).
  2. The visit promise resolves in finishNavigation, after resource.commit(), so only once React has committed the new page (@void/react/dist/runtime/pages-client.mjs:265-277).
  3. Void shows the new page with a dispatch inside its own startTransition (pages-client.mjs:425).
  4. React batches transitions that are pending at the same time: "If there are multiple ongoing Transitions, React currently batches them together" (useTransition caveats). Void's update can't commit until the caller's transition ends.

So the caller's transition waits for action(), action() waits for the commit, and the commit waits for the caller's transition.

Void already avoids this for useForm. VisitOptions has an internal option for exactly this case (void/dist/pages/client.d.mts:44):

/** @internal Used by React Actions to avoid waiting for a commit blocked by the action itself. */
_resolveOnShell?: boolean;

useForm runs inside useActionState (@void/react/dist/index.mjs:288) and passes _resolveOnShell: true (index.mjs:304), so its visit resolves as soon as the page data arrives (pages-client.mjs:358). action() passes only { method, data } (void/dist/pages/client.mjs:1234), so it waits for the commit.

Why it matters

action() is how you call an action when useForm doesn't fit, such as a delete button outside a form. React 19's own ways to show a pending state (useTransition, useActionState, <form action>) are exactly the ones that hang. Apps have to track pending with useState and keep action() out of transitions, and nothing in the docs says so.

Proposal: resolve action() when the page data arrives

Pass _resolveOnShell: true in action(), as useForm does. The result (ok with pageData, or error) is complete by then, and the new page commits when the caller's transition ends. Both ways of calling an action would then behave the same.

Alternatives
  • A public option on action() and router.visit(), e.g. { resolveOn: 'data' }, keeping today's resolve-after-commit as the default for callers that need the new page on screen when the await returns.
  • At least, document it: action() and router.visit() can't be awaited inside a transition, useActionState or a <form action> function.
Lingua principale
TypeScript
Stelle
627
Fork
66
Merge medio
14h 56m
PR unite (30g)
170

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di voidzero-dev/void

Tutte le issue di voidzero-dev/void

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.