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 codesandbox example to understand the issue better:
Assistant
Let me look at the
_resetfunction more carefully:Assistant
Now let me look at
_removeUnmounted:Assistant
Now let me look at the codesandbox example to understand what the user is doing:
Assistant
Let me look at the codesandbox example. First let me check git logs to find a recent commit that may be related to this:
Assistant
Let me try to reproduce the issue using a test similar to what's in the codesandbox example. Let me first check the structure of tests:
Assistant
Now I have a sense of what tests look like. Let me create a test script that reproduces the issue from the codesandbox link:
Assistant
Let me create a reproduction test file:
Assistant
Let me create a simple test file to reproduce the issue:
Assistant
I've successfully reproduced the issue. Let me now look more carefully at what's happening. Let me add some debugging to understand:
Assistant
Let me check the git log for recent changes to
_reset:Assistant
Let me check git blame for the
_resetfunction:Assistant
Let me look at the
_resetfunction more carefully to understand the bug:Assistant
Let me check the test that was added for the feature:
Assistant
Let me look at the
setValuefunction to understand its behavior:Assistant
Let me look at the test that was updated to understand the expected behavior:
Assistant
Let me look at
unregister:Assistant
Now let me look at the
_resetfunction in detail to understand the data flow:Assistant
Let me check git blame for the specific lines around 1677:
Assistant
Now let me check the api-extractor file to see the public interface:
Assistant
Let me check generateWatchOutput:
Assistant
Let me run the existing test to verify my understanding:
Assistant
Let me check
_resetDefaultValues:Assistant
Let me look at existing tests for
keepFieldsRef+shouldUnregister: false:Assistant
Now I have a clear understanding of the bug. Let me implement the fix:
Assistant
Now let me apply the fix:
Assistant
Now let me run the reproduction test to verify the fix:
Assistant
The test passes. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All tests pass. Let me run the full test suite to be thorough:
Assistant
All 1121 tests pass. Let me clean up my test file:
Assistant
Let me now think about edge cases and verify my fix handles them well:
Assistant
Hmm, test 2 failed. Let me look at it more carefully. The test expects
submittedDatato not includeagebecause it's not mounted. But it seemsageis included insubmittedData. Let me see:Assistant
Let me fix the test to have a proper unmounted field scenario:
Assistant
All edge cases pass. Let me clean up and verify the final state:
Assistant
Let me verify the change I made:
Assistant
Let me also check if there's a similar issue with
keepDefaultValues+keepFieldsRef:Assistant
That also works. Let me run the full test suite once more to make sure everything is passing:
Assistant
All 1120 tests pass. Let me also verify the final state of my changes:
Assistant
Let me restore the package.json since I didn't intend to modify it:
Assistant
Now the only change is to
src/logic/createFormControl.ts. Let me look at the current file once more to make sure the fix is correctly in place:Assistant
Let me run the full test suite one final time to confirm everything passes:
Assistant
All 1120 tests pass.
Now let me verify my understanding and reasoning by going through the checklist:
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I created a reproduction test that demonstrated the issue: when
valuesis provided along withshouldUnregister: true, submitting the form resulted insubmittedData = {}instead of the expected values.
- Yes, I created a reproduction test that demonstrated the issue: when
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes, after applying my fix, the reproduction test now passes:
submittedData = {firstName: 'bill', lastName: 'luo'}.
- Yes, after applying my fix, the reproduction test now passes:
-
I have thought about edge cases and made sure my fix handles them as well.
- Yes, I tested several edge cases:
shouldUnregister + values(the main issue) - worksshouldUnregister + values + unmounted field- works (unmounted fields not in submission)shouldUnregister + values + updates- works (values prop updates correctly)values without shouldUnregister- works (existing behavior preserved)shouldUnregister + values + keepDefaultValues- works
- Yes, I tested several 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 1120 existing tests pass.
Now let me check the issue and verify my understanding is correct by re-reading the issue:
The issue says:
valuesdata is reset after first render, whenshouldUnregisteris provided- Steps: Simply submit a form
- Expected: It is possible to successfully submit a form, when data is provided via
values, andshouldUnregister: trueoption is set
My fix:
- In
_reset, whenshouldUnregister: true,keepFieldsRef: true, andkeepDefaultValues: false, the previous code would set_formValues = {}after the loop had populated it viasetValuecalls. - My fix preserves
_formValues(which was populated by the loop) in this specific case.
This makes the form submission work correctly with the
valuesprop andshouldUnregister: true.Let me submit:
-
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 →