[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition
Maintainer antworten meist innerhalb von 2 Tagen
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 78/100
- Issue-Typ
- Dokumentation
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- javascript, react
- Bereich
- documentation
Rechercherichtung
Aktualisiere die Referenzseite zu useFormStatus unter react.dev/reference/react-dom/hooks/useFormStatus. Lies die Abschnitte Returns und Usage und prüfe dann den verlinkten React-PR sowie die Fälle in ReactDOMForm-test.js, um die beschriebenen Bedingungen zu bestätigen. Ergänze die Erklärung zum ausstehenden Status und ein kurzes Beispiel mit onSubmit/preventDefault()/startTransition; fertig ist es, wenn beide erforderlichen Aufrufe und das Verhalten korrekt dokumentiert sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 11.8k
- Forks
- 8k
- Ø Merge
- 2 T. 31 Min.
- Gemergte PRs (30 T.)
- 8
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus reactjs/react.dev
-
[Suggestion]: lazy initialization example can encourage unsafe patterns - show exampleEvtl. vergeben @Mahendra-2006 hat das vor 5 Tagen übernommen. Offentype: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
reactjs/react.dev#8677 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
type: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
reactjs/react.dev#8661 · 1 Kommentar · 7 Reaktionen ·
Maintainer antworten meist innerhalb von 2 Tagen
-
docs: improve useState explanation in quick startEvtl. vergeben @tirthraj01 hat das vor 23 Tagen übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 2 Tagen
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculptureEvtl. vergeben @Saurav10codes hat das vor 32 Tagen übernommen. Offenbug: unconfirmed
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
reactjs/react.dev#8634 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[Typo]: Repeaded words in Pitfall sectionEvtl. vergeben @mdimamhosen hat das vor 34 Tagen übernommen. Offentype: typos
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
reactjs/react.dev#8622 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 2 Tagen
Alle Issues in reactjs/react.dev
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 80/100
-
documentation
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
githubnext/gh-aw-workshop#4251 ·
Maintainer antworten meist innerhalb von 1 Tag
-
customer-support needs-triage Platform(Default)
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Leonxlnx/taste-skill#129 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100