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
Now let me write a reproduction test to confirm the bug:
- Assistant
Bug reproduced —
onSubmitis called with{}. Now let me trace the root cause more precisely: - Assistant
The repro passes. Now run the full test suite to check for regressions:
- Assistant
All 1120 tests pass. Now add a permanent regression test alongside the other
values-prop tests: - Assistant
Full suite once more, plus lint and type checks:
- Assistant
Fixed. Summary of the change:
Root cause
Since #12923, the
valuesprop path inuseFormcalls_reset(props.values, { keepFieldsRef: true })after the first render (src/useForm.ts:155). Inside_reset, thekeepFieldsRefbranch correctly applies the new values viasetValue()for every mounted field — but immediately afterwards, whenshouldUnregister: true,_formValueswas unconditionally wiped to{}.That wipe is only valid for a full reset, where
_fields = {}forces every input to remount and repopulate_formValuesthroughupdateValidAndValueduring re-registration. WithkeepFieldsRefthe field references are preserved, nothing remounts, so_formValuesstayed empty — inputs still displayed their values (the DOM refs were updated), butgetValues()/handleSubmitreturned{}.Fix
src/logic/createFormControl.ts(in_reset):if (!_options.shouldUnregister) { _formValues = cloneObject(values) as TFieldValues; } else if (!keepStateOptions.keepFieldsRef) { // fields are remounted after reset and repopulate _formValues on // register, whereas keepFieldsRef keeps values applied via setValue _formValues = keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues); }When
shouldUnregister+keepFieldsRefare both set,_formValuesis left as populated by thesetValueloop — which also preservesshouldUnregistersemantics (only mounted fields' values are kept, rather than cloning the wholevaluesobject).Verification
- Added two regression tests in
src/__tests__/useForm.test.tsx: submitting withvalues+shouldUnregister: true(the reported repro), and updating thevaluesprop afterwards. - Sanity-checked (throwaway test) that unmounted fields are still dropped from submit data with this combination.
- Full suite: 118 suites / 1122 tests pass;
tsc --noEmitclean; eslint shows only 3 pre-existing prettier errors that exist on the baseline too.
- Added two regression tests in
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- fable-5Compare side by side →