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.
- 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.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /react-hook-form, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
Let me look at the
createFormControl.tsfile to understand the code structure, particularly the parts that deal withshouldUnregisterandvalues.Assistant
Let me look at the section around line 1677 to understand how
_formValuesis updated and the relationship withshouldUnregister.Assistant
Let me look at the start of the
resetfunction and the related context.Assistant
Let me look at the
useForm.tsto understand the initialization process.Assistant
Let me look at the codesandbox example to understand the issue better.
Assistant
Let me look at the specific test for updating form values when
valuesis provided.Assistant
Now let me reproduce the issue.
Assistant
Let me look at the
getValuesfunction and the section that handlesvaluesinitialization:Assistant
Let me look at the relevant section to understand how
_formValuesis initialized and used in the context ofshouldUnregister.Assistant
Now let me create a reproduction script for the issue.
Assistant
I've successfully reproduced the issue. The form is submitted with empty data. Let me look at the
setValuefunction and how_formValuesis updated.Assistant
Let me look at the git history to understand when this code was added.
Assistant
Let me look at the most recent change to the
_resetfunction.Assistant
Let me search for the original commit that introduced this code.
Assistant
Let me look at the original implementation of this code:
Assistant
Let me look at the relevant tests:
Assistant
Now I have a clear understanding. The bug is in the
_resetfunction increateFormControl.ts. WhenshouldUnregister: trueandkeepFieldsRef: trueis used (which is the case whenuseForm({ values })is called), the_formValuesis reset to{}instead of being set to the new values.Let me look at the test cases related to this:
Assistant
Now let me look at the current code more carefully and apply the fix:
Assistant
Now let me apply the fix:
Assistant
The sed command didn't work for multi-line. Let me use a Python script instead:
Assistant
The fix is applied. Now let me run the reproduction test to see if it passes:
Assistant
The reproduction test now passes. Let me also remove this test file since it's a temporary file:
Assistant
Now let me run the existing tests to make sure I didn't break anything:
Assistant
All useForm tests pass. Let me also run all the useForm tests:
Assistant
All 451 tests pass. Let me also run all the tests in the repository:
Assistant
All 1111 tests pass! Let me also test with
TEST_ENV=server:Assistant
All tests pass. Now let me think about edge cases:
Assistant
All edge case tests pass. Let me clean up the test file:
Assistant
Now let me verify the fix again by looking at the change:
Assistant
Let me also verify the issue with the original sandbox scenario again. Let me write a more comprehensive test:
Assistant
The fix is working. Let me clean up the test file:
Assistant
Now let me also think about whether the fix could break any other scenarios. Let me look at the existing tests for
shouldUnregister:Assistant
Let me also run the tests with focus on
shouldUnregister:Assistant
All
shouldUnregistertests pass. Let me also look at the test for the watch test that was modified:Assistant
The test passes. Now let me think about a possible edge case - what about the case when
keepFieldsRef: trueis passed butshouldUnregister: false?Let me trace through:
_options.shouldUnregisterisfalsekeepStateOptions.keepFieldsRefistrue_formValues = keepStateOptions.keepFieldsRef ? (cloneObject(values) as TFieldValues) : ...- Result:
_formValues = cloneObject(values). This is the same as before the fix.
And what about when
keepFieldsRef: falseis passed explicitly (by overriding inuseFormprops)?keepStateOptions.keepFieldsRefisfalse_formValues = _options.shouldUnregister ? ... : (cloneObject(values) as TFieldValues)- If
shouldUnregister: true, then_formValues = keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues). - If
shouldUnregister: false, then_formValues = cloneObject(values). This is the same as before.
Now let me look at the watch test more carefully:
Assistant
Let me check the watch implementation:
Assistant
Let me check the git history of the watch test:
Assistant
Let me run the specific test to see the actual output:
Assistant
Let me add some debugging to understand the test:
Assistant
Let me look at the watch subscription in the form:
Assistant
Let me look at the
useWatchmore carefully:Assistant
Let me look at the
_subscribefunction:Assistant
Let me look at
shouldRenderFormState:Assistant
Let me look at the register function to understand the input value:
Assistant
Let me also check that the fix works for
defaultValueswithshouldUnregister:Assistant
All edge case tests pass. Let me clean up:
Assistant
Now let me run all the tests one more time to make sure everything still passes:
Assistant
Let me also verify the final state of the fix:
Assistant
Let me also check the git status:
Assistant
Let me run the tests one more time to confirm everything still works:
Assistant
All 1111 tests pass. Let me also verify the diff once more:
Assistant
Let me do a final review of the change:
Assistant
The fix is complete. Let me address the checklist:
Checklist Review:
-
If an error was reported in the issue description, I have successfully reproduced it.
- ✅ YES. I created a temporary test file that reproduced the issue: when
useForm({ values, shouldUnregister: true })is used and the form is submitted, the form data is empty{}instead of containing the values.
- ✅ YES. I created a temporary test file that reproduced the issue: when
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- ✅ YES. After applying the fix, the reproduction test passes - the form submits with the correct values.
-
I have thought about edge cases and made sure my fix handles them as well.
- ✅ YES. I tested the following edge cases:
- The original issue:
useForm({ values, shouldUnregister: true })and submit useForm({ values, shouldUnregister: true })with nested values (e.g.,nested.field)useForm({ values, shouldUnregister: true })with values that update via setStateuseForm({ defaultValues, shouldUnregister: true })and submituseForm({ defaultValues, shouldUnregister: true })and callreset()then submit
- The original issue:
- ✅ YES. I tested the following edge cases:
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
- ✅ YES. All 1111 tests in the repository pass with my fix applied.
Summary of the fix:
The bug was in
_resetfunction insrc/logic/createFormControl.ts. WhenshouldUnregister: trueand the_resetfunction is called withkeepFieldsRef: true(which is the case whenuseForm({ values })is used), the_formValueswas being reset to{}instead of being set to the new values. This meant the form data was empty when submitted.The fix adds a check for
keepFieldsRef: truebefore theshouldUnregistercheck. WhenkeepFieldsRef: true, the_formValuesis set to the new values (cloned) regardless of whethershouldUnregister: true. This is becausekeepFieldsRef: trueindicates that the values are being updated (typically via theuseForm({ values })prop), and the field references are being kept. In this case, the new values should be used.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →