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 check where
keepFieldsRefis used and understand the reset flow: - Assistant
Now let me reproduce the issue with a test. First check existing tests around the
valuesprop: - Assistant
The fix works. Now run the full test suite to check for regressions:
- Assistant
All 1121 tests pass. Let me also run the type check and lint:
- Assistant
Those 3 prettier errors are at lines 1114, 1188, 1525 — nowhere near my edit (~line 1677). Let me confirm they pre-exist:
- Assistant
Confirmed pre-existing — those errors exist at HEAD too (prettier version drift in this environment), so I've left them untouched. My changed lines are lint-clean.
Summary
Root cause (
src/logic/createFormControl.ts): WhenuseFormreceives avaluesprop, an effect syncs it via_reset(props.values, { keepFieldsRef: true })after the first render. Inside_reset, thekeepFieldsRefbranch correctly writes the new values into_formValuesviasetValue, but the line right after unconditionally wiped_formValuesto{}whenevershouldUnregister: true.That wipe is normally fine: without
keepFieldsRef,_fieldsis cleared, so every input re-registers on the next render andupdateValidAndValuerepopulates from . But with , fields stay mounted (), so skips and the ref callback early-returns — stays empty forever, and submits .
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.