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
I'll investigate how
valuesandshouldUnregisterinteract in the codebase to find why data resets after the first render. - Assistant
I'll add a failing test for
values+shouldUnregister, then fix the_resetlogic that clears_formValues. - Assistant
Fixed a bug where
valueswas wiped after the first render whenshouldUnregister: truewas set.Root cause
When the
valuesprop syncs inuseForm, it calls_resetwithkeepFieldsRef: true. That path correctly updates each mounted field viasetValue, but then_resetoverwrote_formValueswith{}whenevershouldUnregisterwas true:_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : keepStateOptions.keepFieldsRef ? _formValues : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);Because
handleSubmitreads from_formValuesdirectly, the form submitted empty data after the post-mountvalueseffect ran.Fix
When
shouldUnregisteris true andkeepFieldsRefis true, preserve_formValuesafter thesetValueloop instead of resetting it to{}. That keeps only registered field values, which matches semantics.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.