[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition
Los mantenedores suelen responder en 4 días
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 78/100
- Tipo de issue
- Documentación
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- javascript, react
- Área
- documentation
Línea de trabajo
Actualiza la página de referencia de useFormStatus en react.dev/reference/react-dom/hooks/useFormStatus. Lee las secciones Returns y Usage y, después, comprueba el PR de React enlazado y los casos de ReactDOMForm-test.js para confirmar las condiciones descritas. Añade la explicación del estado pendiente y un ejemplo breve de onSubmit/preventDefault()/startTransition; estará terminado cuando ambas llamadas requeridas y el comportamiento estén documentados con precisión.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- JavaScript
- Estrellas
- 11.8k
- Forks
- 8k
- Merge medio
- 5 d 11 h
- PR fusionados (30 d)
- 5
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de reactjs/react.dev
-
[Suggestion]: lazy initialization example can encourage unsafe patterns - show examplePosiblemente ocupada @Mahendra-2006 la tomó hace 9 días. Abiertotype: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
reactjs/react.dev#8677 · 2 comentarios ·
Los mantenedores suelen responder en 4 días
-
type: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
reactjs/react.dev#8661 · 1 comentario · 7 reacciones ·
Los mantenedores suelen responder en 4 días
-
docs: improve useState explanation in quick startPosiblemente ocupada @tirthraj01 la tomó hace 28 días. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
Los mantenedores suelen responder en 4 días
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculpturePosiblemente ocupada @Saurav10codes la tomó hace 36 días. Abiertobug: unconfirmed
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
reactjs/react.dev#8634 · 3 comentarios ·
Los mantenedores suelen responder en 4 días
-
[Typo]: Repeaded words in Pitfall sectionPosiblemente ocupada @mdimamhosen la tomó hace 38 días. Abiertotype: typos
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
reactjs/react.dev#8622 · 1 comentario ·
Los mantenedores suelen responder en 4 días
Todos los issues de reactjs/react.dev
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
druxt/umami.demo.druxtjs.org#527 ·
Los mantenedores suelen responder en 9 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
NuSkooler/enigma-bbs#907 ·
Los mantenedores suelen responder en 1 día
-
documentation good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
documentation good first issue help wanted
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día