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
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.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Assistant
Let me start by understanding the repository structure and the issue. The issue is about
valuesdata being reset after the first render whenshouldUnregisteris provided. This is in thereact-hook-formlibrary.Let me explore the repository first.
Assistant
Let's explore the source code. This is react-hook-form, a React form library. Let's find the relevant files.
Assistant
Let me look at the logic directory and the form creation logic
Assistant
Let me look at the
createFormControl.tsfile to understand the main form control logic, especially aroundvaluesandshouldUnregister.Assistant
Let me look at the relevant code sections around these lines.
Assistant
So I can see:
_defaultValuesis set from_options.defaultValues || _options.values_formValuesis{}ifshouldUnregisteris true, elsecloneObject(_defaultValues)
This means when
shouldUnregisteris true,_formValuesstarts empty. But the user providesvaluesto pre-populate the form. Let me look at howvaluesis handled later.Let me search for where
valuesoption is handled:Assistant
Let me also search for how
valuesis used moreAssistant
Let me look at the area around line 1670-1710 which seems to be the reset logic where
_formValuesgets set:Assistant
Let me look at
useForm.tsfor the initial setupAssistant
Let me check
_resetDefaultValuesfunctionAssistant
Let me look at lines around 1799.
Assistant
Let me look at the
_getWatchfunctionAssistant
Let me look at the
_getWatchfunction:Assistant
Let me look at the
handleSubmitfunction and how values are gathered forAssistant
Let me look at the submission path
Assistant
Let me look at the
registerfunctionAssistant
Let me look at the register function:
Assistant
Let me look at `updateValidAndValue
Assistant
Let me see
updateValidAndValue:Assistant
Let me check the initial
_statevalueAssistant
Let me check the git log for recent changes related to this area
Assistant
Let me look at recent git changes to
createFormControl.tsAssistant
Let me look at the most recent commit
Assistant
Let me look at the diff of that
Assistant
Let me search for existing tests related to this
Assistant
Let me look at the reset tests for shouldUnregister with
Assistant
Let me search for relevant test cases
Assistant
Let me look at the test case around line 992
Assistant
Let me look at the test at line 1037
Assistant
Let me look at the Controller component
Assistant
Let me look at
useController.ts:Assistant
Now I have a good understanding of the issue. Let me create a reproduction script first to confirm the bug
Assistant
Let me look at the test structure and try to write a simple
Assistant
Let me check if there's already a test covering the
valuesprop withshouldUnregisterinuseForm.test.tsxAssistant
Let me look at the test around line 2276
Assistant
Now I have a clear understanding of the issue. Let me trace through the code:
- When
shouldUnregister: true,_formValuesstarts as{}and gets populated when fields register - The
useFormvaluesuseEffectcalls_reset(props.values, { keepFieldsRef: true, ... }) - In
_reset,setValueis called for each mounted field (correctly populating_formValues) - But then
_formValues = {}is assigned becauseshouldUnregisteris true, clearing everything!
The
setValuecalls in thekeepFieldsRefbranch correctly populate_formValues, but the assignment right after wipes them out. Let me verify this by looking at the exact- When
Assistant
Let me also check the unregister function to understand how fields are removed from `_formValues
Assistant
Let me look at the unregister function
Assistant
Let me check
_removeUnmounted:Assistant
Let me look at
_removeUnmountedAssistant
Now I'm confident about the fix. In
_reset, whenkeepFieldsRefis true andshouldUnregisteris true, thesetValueloop correctly populates_formValues, but then the assignment_formValues = {}wipes it all out. The fix is to preserve_formValueswhenkeepFieldsRefis true.Let me implement the fix
Assistant
Let me open the file properly:
Assistant
Now I'll apply the fix. The issue is that when
keepFieldsRefis true (which is passed from theuseFormvalues effect), thesetValueloop correctly populates_formValues, but then it's immediately cleared to{}becauseshouldUnregisteris true. The fix preserves_formValueswhenkeepFieldsRefis true since it was already correctly populated.Assistant
Let me use the proper heredoc syntax for the edit command
Assistant
Let me try a different approach to the edit command
Assistant
Good. Now let me verify the change and run the relevant
Assistant
Now let me run the existing tests to verify nothing is broken. Let me start with the reset tests
Assistant
All 39 reset tests pass. Let me run the rest of the useForm tests
Assistant
All 347 useForm tests pass. Let me also run the other test suites that might be affected
Assistant
All tests pass. Now let me create a reproduction script to confirm the fix resolves the exact issue described
Assistant
Let me run this test to verify the fix
Assistant
Let me put the test in the proper location
Assistant
Let me check the jest
Assistant
It should match
**/__tests__/**/*.(spec|test).ts?(x). The file I created issrc/__tests__/issue-test.tsxwhich matches the pattern. Let me check if the file exists:Assistant
Let me try running with verbose output
Assistant
The pattern requires the filename to end with
.test.tsxor.spec.tsx. Let me rename itAssistant
Let me fix the import path
Assistant
Let me look at how other tests import
Assistant
Let me look at the import for useForm in a test file:
Assistant
Let me fix the test file with proper imports
Assistant
The import has double single-quotes. Let me fix it:
Assistant
Now let me run the
Assistant
Both tests pass! The fix is working. Let me clean up the test file and run all useForm tests once more to confirm everything passes
Assistant
All 347 tests pass. Let me also run the remaining relevant test suites
Assistant
Let me run all tests to be thorough
Assistant
All 1120 tests pass across 118 test suites. The fix is solid and minimal. Let me look at the final diff
Assistant
There's an unintended change to
package.json. Let me revertAssistant
Let me verify the final diff only contains my intended
Assistant
Perfect. Let me also make sure the test file is cleaned up
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.