[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition
メンテナーはふだん 2 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- ドキュメント
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- javascript, react
調査の方向性
react.dev/reference/react-dom/hooks/useFormStatus の useFormStatus リファレンスページを更新してください。Returns と Usage のセクションを読み、リンクされている React PR と ReactDOMForm-test.js のケースを確認して、説明されている条件を検証してください。保留状態の説明と、onSubmit/preventDefault()/startTransition を使った短い例を追加してください。必要な 2 つの呼び出しと動作が正確に文書化されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
Document that useFormStatus also reports pending: true when a submit event handler calls event.preventDefault() and starts a Transition with startTransition, not only when the form uses the action prop.
Page
https://react.dev/reference/react-dom/hooks/useFormStatus
Details
I'm implementing a custom <Form> component that accepts a Transition-based prop like submitAction (it validates and transforms the values, then calls submitAction inside startTransition). I wanted to provide the form's pending state through useFormStatus itself, instead of adding a custom context. While investigating, I found that this already works, but because it is not documented, I could not tell whether it is safe to rely on.
The useFormStatus page currently describes the status only in terms of the action prop on the parent <form>. However, since https://github.com/facebook/react/pull/29019 (shipped in React 19.0.0), useFormStatus is also activated when startTransition is called inside a submit event handler that is preventDefault-ed:
After this change,
useFormStatuswill also be activated if you callstartTransition(actionFn)inside a submit event handler that ispreventDefault-ed.
The PR describes this as the way for UI component libraries to implement a custom action prop with onSubmit, and the behavior is covered by tests in ReactDOMForm-test.js:
- useFormStatus is activated if startTransition is called inside preventDefault-ed submit event
- useFormStatus is not activated if startTransition is not called
- useFormStatus is not activated if event is not preventDefault-ed
This matters for form components like mine that cannot use the action prop, for example because they validate and transform values before calling the action, or because they do not want the automatic form reset.
I confirmed the behavior with React 19.3.0 in Chrome.
Playground: https://codesandbox.io/p/sandbox/custom-useformstatus-fm2fkl
// App.js
import { startTransition, useActionState } from "react";
import { useFormStatus } from "react-dom";
// A fake request that takes 2 seconds.
function save(value) {
return new Promise((resolve) => setTimeout(() => resolve(value), 2000));
}
// A form component in a UI library. It does not use the `action` prop,
// e.g. because it validates and transforms the values before calling the action.
function MyForm({ submitAction, children }) {
return (
<form
onSubmit={(event) => {
event.preventDefault();
const value = new FormData(event.currentTarget).get("name");
startTransition(async () => {
await submitAction(value);
});
}}
>
{children}
</form>
);
}
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? "Sending" : "Send"}
</button>
);
}
function App() {
const [state, sendAction] = useActionState(async (prev, value) => {
await save(value);
return value;
}, null);
return (
<MyForm submitAction={sendAction}>
<input name="name" />
<SubmitButton />
{state && <p>Saved: {state}</p>}
</MyForm>
);
}
export default App;
pending stays true until the action finishes, and the form is not reset.
Suggested changes:
- In "Returns", say that
pendingis alsotruewhile a Transition started from apreventDefault-ed submit event of the parent<form>is pending. - In "Usage", add a short example of the
onSubmit+preventDefault()+startTransitionpattern, and note that bothpreventDefault()andstartTransitionin the same event are required.
- 主要言語
- JavaScript
- スター
- 11.8k
- フォーク
- 8k
- 平均マージ
- 2日 31分
- マージ済み PR(30日)
- 8
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
reactjs/react.dev のほかの issue
-
[Suggestion]: lazy initialization example can encourage unsafe patterns - show example対応中かも @Mahendra-2006 が 5 日前に担当しました。 オープンtype: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
reactjs/react.dev#8677 · コメント 2 件 ·
メンテナーはふだん 2 日以内に返信
-
type: documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
reactjs/react.dev#8661 · コメント 1 件 · リアクション 7 件 ·
メンテナーはふだん 2 日以内に返信
-
docs: improve useState explanation in quick start対応中かも @tirthraj01 が 24 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculpture対応中かも @Saurav10codes が 32 日前に担当しました。 オープンbug: unconfirmed
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
reactjs/react.dev#8634 · コメント 3 件 ·
メンテナーはふだん 2 日以内に返信
-
[Typo]: Repeaded words in Pitfall section対応中かも @mdimamhosen が 34 日前に担当しました。 オープンtype: typos
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
reactjs/react.dev#8622 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
reactjs/react.dev の issue をすべて見る
似ている issue
-
macOS
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
beyondcode/herd-community#1761 ·
-
content good first issue hacktoberfest
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
vieanderes/understory#19 ·
-
automated issue report
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
lirantal/discoprint#37 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
tauri-apps/tauri#16219 ·
メンテナーはふだん 2 日以内に返信
-
agentic-workflows
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
githubnext/gh-aw-cao#16599 ·
メンテナーはふだん 1 日以内に返信