React `useForm`: `reset()` restores the last submitted values, not the initial ones
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 52/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bastante claro
- Estado de actividad
- Activo
- Stack tecnológico
- react, typescript
- Área
- frontend
Línea de trabajo
Read packages/react/src/runtime/use-form.ts around the successful submitAction path and the reset() implementation, then check the useForm reset documentation. Decide how the API should restore the original values while preserving the current behavior for edit forms; done means create forms can reset to their initial values and the documented behavior matches the implementation.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Version
void / @void/react 1.1.12
What happens
After a successful submit, useForm replaces its defaults with the submitted data (packages/react/src/runtime/use-form.ts, after submitAction returns ok):
const nextDefaults = { ...dataRef.current };
defaultsRef.current = nextDefaults;
setDefaultsState(nextDefaults);
So calling reset() after a successful submit restores the values that were just submitted. The docs describe reset(...fields) as "Reset form data to initial values", which suggests the values passed to useForm.
Reproduction
const form = useForm('/notes?create', { title: '', body: '' });
<form action={form.post}>
<input value={form.data.title} onChange={(e) => form.setData('title', e.target.value)} />
<button>Add</button>
</form>
- Type "Hello" and submit. The action succeeds.
- Call
form.reset(), for example from an effect onform.wasSuccessful. - Expected:
titleis''. Actual:titleis"Hello".
Why it matters
For "create" forms, clearing the fields after a successful submit is the most common need (add a note, add a todo, send a message). Today that requires keeping a separate copy of the original defaults outside useForm.
Promoting submitted values to defaults makes sense for "edit" forms (hasChanges goes back to false after saving), so I'm not suggesting that behavior should change, only that there should be a way to get back to the original values.
Possible fix
- a
resetOnSuccess: trueoption onuseForm, or - a way to reset to the original values, e.g.
reset({ initial: true })orresetToInitial().
At minimum, the reset() docs could say that submitted values become the new defaults after a successful submit.
- Lenguaje dominante
- TypeScript
- Estrellas
- 402
- Forks
- 32
- Merge medio
- 8 h 32 min
- PR fusionados (30 d)
- 111
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
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 voidzero-dev/void
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
voidzero-dev/void#565 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
voidzero-dev/void#529 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 1/5 1-3 horas Aptitud para principiantes 78/100
voidzero-dev/void#428 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 53/100
voidzero-dev/void#567 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
voidzero-dev/void#564 ·
Los mantenedores suelen responder en 1 día
Todos los issues de voidzero-dev/void
Issues similares
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 82/100
source-cooperative/docs.source.coop#41 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Nimblesite/SharpLsp#468 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
solidjs-community/solid-primitives#1082 ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día