issue: values data is reset after first render, when shouldUnregister is provided…
- User
issue:
valuesdata is reset after first render, whenshouldUnregisteris providedVersion Number
7.71.2
Codesandbox/Expo snack
https://codesandbox.io/p/sandbox/blazing-dust-3225h9?file=%2Fsrc%2FApp.js
Steps to reproduce
- Simply submit a form
- See error
Expected behaviour
It is possible to successfully submit a form, when data is provided via
values, andshouldUnregister: trueoption is setWhat browsers are you seeing the problem on?
Chrome
Code of Conduct
- I agree to follow this project's Code of Conduct
Interface
No new interfaces are introduced.
- Assistant
Investigating how
valuesandshouldUnregisterinteract in the codebase. - Assistant
Reproducing the bug with a test, then tracing the reset flow.
- Assistant
Implementing the fix:
_resetclears_formValuesaftersetValuewhenkeepFieldsRefandshouldUnregisterare both set. - Assistant
Fixed a bug where
valueswas wiped after the first render whenshouldUnregister: truewas set.Root cause
When
useFormreceives avaluesprop, it calls_resetwithkeepFieldsRef: trueso mounted fields keep their refs. WithshouldUnregister: true,_resetwas:- Calling
setValuefor each mounted field (populating_formValues) - Then immediately resetting
_formValuesto{}
That left the internal form state empty after the first render, so
getValues()andhandleSubmitreturned{}even though the inputs still showed the correct values. - Calling
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.