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

已關閉
#643 0 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

維護者通常 1 天內回覆

@cpojer 已經在處理了。

開始於 2026年10月30日。

評估

這個 Issue 還沒有評估資料。

描述

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.
主要語言
TypeScript
星號
627
分支
66
平均合併
14 小時 56 分鐘
30 天內合併 PR
170

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

voidzero-dev/void 的其他 Issue

查看 voidzero-dev/void 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。