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 repro test to confirm the bug:
- Assistant
Bug reproduced. Now let me trace the flow in
createFormControl: - Assistant
The repro passes. Now run the full test suite to check for regressions:
- Assistant
All 1121 tests pass. Let me move the repro into the proper test file as a regression test — first find where
valuesprop tests live: - Assistant
Now add a proper regression test in
useForm.test.tsxand remove the temp file: - Assistant
All 1121 tests pass. Run type check and lint on the changed files:
- Assistant
Pre-existing lint issues, not mine. Verify tsc separately:
- Assistant
The fix is complete. Note: the
package.jsonmodification was already present before I started (per the initial git status) — my changes are only increateFormControl.tsand the test file.Summary
Root cause (
src/logic/createFormControl.ts:1677): Whenvaluesis passed touseForm, an effect calls_reset(props.values, { keepFieldsRef: true })after the first render. Inside_reset, thekeepFieldsRefbranch correctly writes the new values into_formValuesviasetValue, but immediately afterward theshouldUnregisterbranch wiped_formValuesback to{}. That wipe relies on fields re-registering (which repopulates_formValuesfrom_defaultValues) — but withkeepFieldsRef: truethe field refs are kept and takes the early "already registered" path, so stayed empty forever. Submitting then failed validation / produced empty data.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.