[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Documentazione
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- javascript, react
- Ambito
- documentation
Direzione di ricerca
Aggiorna la pagina di riferimento di useFormStatus all’indirizzo react.dev/reference/react-dom/hooks/useFormStatus. Leggi le sezioni Returns e Usage, quindi controlla la PR di React collegata e i casi in ReactDOMForm-test.js per confermare le condizioni descritte. Aggiungi la spiegazione dello stato in sospeso e un breve esempio con onSubmit/preventDefault()/startTransition; il lavoro è completato quando entrambe le chiamate richieste e il comportamento sono documentati accuratamente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 8k
- Merge medio
- 2g 31m
- PR unite (30g)
- 8
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di reactjs/react.dev
-
[Suggestion]: lazy initialization example can encourage unsafe patterns - show exampleForse già presa @Mahendra-2006 l’ha presa 4 giorni fa. Apertatype: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
reactjs/react.dev#8677 · 2 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
type: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
reactjs/react.dev#8661 · 1 commento · 7 reazioni ·
I maintainer di solito rispondono entro 2 giorni
-
docs: improve useState explanation in quick startForse già presa @tirthraj01 l’ha presa 23 giorni fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculptureForse già presa @Saurav10codes l’ha presa 32 giorni fa. Apertabug: unconfirmed
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
reactjs/react.dev#8634 · 3 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
[Typo]: Repeaded words in Pitfall sectionForse già presa @mdimamhosen l’ha presa 34 giorni fa. Apertatype: typos
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
reactjs/react.dev#8622 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di reactjs/react.dev
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
QuantEcon/lecture-python-programming#642 ·
I maintainer di solito rispondono entro 1 giorno
-
Missing repro Platform: Android
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
software-mansion/react-native-reanimated#10816 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
area:docs bug triage:confirmed
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
Cotal-AI/Cotal#2875 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
keithamus/css-minify-tests#304 ·
I maintainer di solito rispondono entro 1 giorno
-
[v10] Docs: a not-awaited `$$()` is not a Promise anymoreForse già presa @sh011 l’ha presa oggi. ApertaDocs 📖 good first pick help wanted v10
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
webdriverio/webdriverio#15935 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno