[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Documentation
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, react
- Domain
- documentation
Research direction
Update the useFormStatus reference page at react.dev/reference/react-dom/hooks/useFormStatus. Read its Returns and Usage sections, then check the linked React PR and ReactDOMForm-test.js cases to confirm the described conditions. Add the pending-state explanation and a short onSubmit/preventDefault()/startTransition example; done when both required calls and the behavior are documented accurately.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 8k
- Avg merge
- 2d 31m
- Merged PRs (30d)
- 8
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from reactjs/react.dev
-
[Suggestion]: lazy initialization example can encourage unsafe patterns - show examplePossibly taken @Mahendra-2006 claimed this 4 days ago. Opentype: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
reactjs/react.dev#8677 · 2 comments ·
Maintainers usually reply within 2 days
-
type: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
reactjs/react.dev#8661 · 1 comment · 7 reactions ·
Maintainers usually reply within 2 days
-
docs: improve useState explanation in quick startPossibly taken @tirthraj01 claimed this 23 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 82/100
Maintainers usually reply within 2 days
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculpturePossibly taken @Saurav10codes claimed this 32 days ago. Openbug: unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
reactjs/react.dev#8634 · 3 comments ·
Maintainers usually reply within 2 days
-
[Typo]: Repeaded words in Pitfall sectionPossibly taken @mdimamhosen claimed this 34 days ago. Opentype: typos
Difficulty 1/5 Under an hour Newbie friendliness 90/100
reactjs/react.dev#8622 · 1 comment ·
Maintainers usually reply within 2 days
All issues in reactjs/react.dev
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
QuantEcon/lecture-python-programming#642 ·
Maintainers usually reply within 1 day
-
Missing repro Platform: Android
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
software-mansion/react-native-reanimated#10816 · 2 comments ·
Maintainers usually reply within 1 day
-
area:docs bug triage:confirmed
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Cotal-AI/Cotal#2875 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
keithamus/css-minify-tests#304 ·
Maintainers usually reply within 1 day
-
[v10] Docs: a not-awaited `$$()` is not a Promise anymorePossibly taken @sh011 claimed this today. OpenDocs 📖 good first pick help wanted v10
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
webdriverio/webdriverio#15935 · 3 comments ·
Maintainers usually reply within 1 day