ISSUE DESCRIPTION issue: values data is reset after first render, when shouldUnregister…
- User
ISSUE DESCRIPTION
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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
Assistant
Let me start by exploring the repository structure to understand the codebase and then reproduce the issue.
Assistant
This is the react-hook-form library. The issue is about
valuesdata being reset after the first render whenshouldUnregisteris provided. Let me explore the source code and understand the issue better.Let me first look at the
useForm.tsfile and the logic directory since those are likely where the bug is.Assistant
The main logic is in
createFormControl.ts. Let me look at that file since it's the largest and likely contains the core form handling logic, including howvaluesare handled withshouldUnregister.Assistant
Let me search for relevant parts of this file related to
valuesandshouldUnregister:Assistant
Now let me look at the specific lines where
_defaultValuesis set during reset, since the issue is aboutvaluesbeing reset after first render whenshouldUnregisteris true. Let me look at the area around line 1619-1630 more carefully:Assistant
Let me look at the handleSubmit function to understand how values are collected during submission:
Assistant
Let me look at the register function to understand how fields are registered with values:
Assistant
Let me look at the register function:
Assistant
Now I need to look at the register function to understand how it handles values. Let me look at it:
Assistant
Let me look at the register function specifically.
Assistant
Now let me look at the
registerfunction which is the main function that registers fields:Assistant
Now let me understand the issue better by looking at how
valuesare used in the initialization:Assistant
Now let me understand the repository structure better and look for existing tests related to this issue:
Assistant
Let me search for tests related to
shouldUnregisterandvalues:Assistant
Let me look at the existing tests related to
valuesandshouldUnregister:Assistant
The command failed because I need to properly escape the pattern. Let me try a different approach.
Assistant
Let me look at these tests around line 227 to understand how shouldUnregister is tested.
Assistant
Let me search more specifically for tests related to
valuesprop:Assistant
Now I understand the repository structure. Let me look at the specific part of the code that handles
valueswithshouldUnregister:Assistant
Now let me understand the issue better. Let me look at how
valuesis handled whenshouldUnregisteris true, and look at the relevantuseForm.tscode:Assistant
Now I understand the repository structure. This is the
react-hook-formlibrary. Let me look at the key files to understand the issue withvaluesandshouldUnregister.Assistant
Now I understand the issue. Let me analyze the bug:
-
When
useFormis called withvaluesandshouldUnregister: true:- In
createFormControl.tsline 1677-1682, when_resetis called withkeepFieldsRef: true(which happens whenvaluesprop changes), the code first populates values viasetValuecalls at line 1665-1672 - But then immediately at line 1677-1682,
_formValuesis reset to{}whenshouldUnregisteris true
- In
-
The fix should preserve values in
_formValueswhenkeepFieldsRefis true, because those fields remain registered and should have their values.
Let me first create a reproduction script to confirm the bug:
-
Assistant
Let me explore the repository structure to understand the issue better, then reproduce and fix it.
Assistant
Let me look at the
_resetfunction more carefully to understand the issue withvaluesandshouldUnregister:Assistant
Now I understand the issue. Let me trace through the code:
-
When
useFormis called withvaluesandshouldUnregister: true, theuseEffectinuseForm.tscallscontrol._reset(props.values, { keepFieldsRef: true, ... }) -
In
_resetfunction (createFormControl.ts), whenkeepFieldsRefis true (line 1664-1671), it callssetValuefor each mounted field, which populates_formValues -
BUT THEN, at line 1677-1682, when
shouldUnregisteris true,_formValuesis RESET to{}(empty object)
The bug: values are first set via
setValue, then immediately overwritten to{}whenshouldUnregisteris true.Let me create a reproduction script first to confirm this issue:
-
Assistant
Now I understand the issue better. Let me trace through the code:
-
When
useFormis called withvaluesandshouldUnregister: true, theuseEffectinuseForm.tscallscontrol._reset(props.values, { keepFieldsRef: true, ... }) -
In
_resetfunction (createFormControl.ts), whenkeepFieldsRefis true (line 1664-1671), it callssetValuefor each mounted field, which populates_formValues -
BUT THEN, at line 1677-1682, when
shouldUnregisteris true,_formValuesis RESET to{}(empty object)
The bug is clear: values are first set via
setValue, then immediately overwritten to{}whenshouldUnregisteris true.Let me create a reproduction script to confirm this issue, then fix it.
-
Assistant
Let me run existing tests to make sure they pass first:
Assistant
Good, the tests pass. Let me look more carefully at the issue. Let me trace the code path when
valuesprop is used withshouldUnregister: true:Assistant
Now I need to understand the issue more precisely. Let me trace through what happens:
- In
useForm.ts, whenvalueschanges, it calls:
control._reset(props.values, { keepFieldsRef: true, ...control._options.resetOptions, });- In
Assistant
Let me run the existing tests first to confirm they pass, then apply my fix.
Assistant
Now I understand the issue clearly. Let me trace through what happens:
-
When
useFormis called withvaluesandshouldUnregister: true, theuseEffectinuseForm.tscallscontrol._reset(props.values, { keepFieldsRef: true, ... }) -
In
_reset, whenkeepFieldsRefis true:- Lines 1665-1671: For each mounted field,
setValueis called, which sets values in_formValues - Lines 1677-1682: BUT then, if
_options.shouldUnregisteris true,_formValuesis set to{}(empty object)
- Lines 1665-1671: For each mounted field,
The problem is: values are first set via
setValue, then immediately overwritten to{}whenshouldUnregisteris true.The fix: When
keepFieldsRefis true, we should preserve values in_formValuesbecause the fields remain registered (we're keeping field references).Let me apply the fix:
-
Assistant
Let me edit the file to apply the fix.
Assistant
Now I need to edit the file to fix the bug. Let me use the
replacecommand to make the change.Assistant
<tool_calls> <parameter=command> open /react-hook-form/src/logic/createFormControl.ts </tool_call>
Assistant
Let me use the edit command to make the fix.
Assistant
<toolfunction=bash_terminal> <parameter=command> cd /react-hook-form && sed -i '1677,1681c\ _formValues = _options.shouldUnregister\n ? keepStateOptions.keepDefaultValues\n ? (cloneObject(_defaultValues) as TFieldValues)\n : keepStateOptions.keepFieldsRef\n ? (cloneObject(values) as TFieldValues)\n : ({} as TFieldValues)\n : (cloneObject(values) as TFieldValues);' src/logic/createFormControl.ts </tool_call>
Assistant
Let me try again with a proper bash command to edit the file.
Assistant
Let me try using the
replacecommand instead.Assistant
Let me use a different approach - use Python to make the edit:
Assistant
Let me try with node instead:
Assistant
Let me verify the change was made correctly:
Assistant
The change was applied successfully. Now let me run the existing tests to make sure they still pass:
Assistant
Existing tests pass. Now let me run more comprehensive tests to make sure I haven't broken anything:
Assistant
The existing tests pass. Now let me run all tests to make sure everything works:
Assistant
All 118 test suites (1120 tests) pass with my fix. Let me verify the change I made:
Assistant
The fix has been applied successfully. Let me summarize what the issue was and how it was fixed:
Issue Analysis
When
useFormis called withvaluesprop andshouldUnregister: true, the following happens:- In
useForm.ts, whenvaluesprop changes, it callscontrol._reset(props.values, { keepFieldsRef: true, ... }) - In
_resetfunction (createFormControl.ts), whenkeepFieldsRefis true:- For each mounted field,
setValueis called, which populates_formValues - BUT then, when
_options.shouldUnregisteris true,_formValuesis reset to{}(empty object)
- For each mounted field,
The bug: values are first set via
setValue, then immediately overwritten to{}whenshouldUnregisteris true.- In
Assistant
Let me try the submit command again.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.