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
Let me write a reproduction test to confirm the diagnosis.
- Assistant
Now let me write a reproduction test:
- Assistant
Bug reproduced. Now let me look at the
handleSubmitand setValue paths to design the fix: - Assistant
All 1121 tests pass. Now let me move the repro test into the proper test file as a permanent regression test:
- Assistant
The 3 prettier errors (lines 1114, 1188, 1525) are outside my changed ranges. Let me verify they pre-exist:
- Assistant
The fix is complete. Summary:
Root cause
When
useForm({ values, shouldUnregister: true })mounts, the effect inuseForm.ts:154calls_reset(props.values, { keepFieldsRef: true }). Inside_reset(src/logic/createFormControl.ts), the order of operations broke the form state:- The
keepFieldsRefloop ransetValue()for each mounted field, populating_formValuesand the DOM refs - Then
_formValueswas reassigned to{}(becauseshouldUnregister: truewithoutkeepDefaultValues), destroying what step 1 just set - Normally the empty
_formValuesgets repopulated when fields re-register via — but keeps intact with , so re-registration takes the "field already exists" path and never repopulates values
- The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.