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
The issue is about
valuesdata being reset after the first render whenshouldUnregister: trueis provided. Let me start by exploring the repository structure to understand the codebase.Let me first look at the repository structure and then try to understand the issue.
Assistant
Let me look at the source directory to understand the codebase structure.
Assistant
Let me look at the key files involved. First, let's examine
useForm.tsand the logic directory.Assistant
Let me look at the logic directory and understand the key functions involved.
Assistant
Let me look at the
createFormControl.tsfile, specifically the_resetDefaultValuesfunction and_resetfunction.Assistant
Let me look at the
_resetDefaultValuesfunction.Assistant
Let me look at the key part of
createFormControl.ts- the initialization and howshouldUnregisterandvaluesinteractAssistant
Let me look at the initialization of
_formValuesand_defaultValuesand the relevant sections:Assistant
Let me look at the
_resetfunction:Assistant
Let me look at the
setValuefunction:Assistant
Let me look at
setValue.Assistant
Let me find
_setValue.Assistant
Let me read the
_setValuefunction.Assistant
Let me look at
_removeUnmountedAssistant
Let me read
_removeUnmounted.Assistant
Let me look at
getValuesfunction:Assistant
Let me check if there are existing tests related to this issue:
Assistant
Let me search in the test files for relevant tests
Assistant
Let me look at the tests around "shouldUnregister set to true" and also create a reproduce script:
Assistant
Let me look at the reset test with shouldUnregister: true
Assistant
Let me look at
generateWatchOutput:Assistant
Let me look at the
registerfunction to see how it handles setting initial values:Assistant
Let me read the
registerfunctionAssistant
Let me find
updateValidAndValue:Assistant
Let me read `updateValidAndValue
Assistant
Let me create a simple reproduction script to test the issue
Assistant
Let me search for tests about
valuesprop combined withshouldUnregisterAssistant
Let me look at the app directory for examples
Assistant
Let me look at the app source
Assistant
Let me look at how the subscription handles state updates
Assistant
Let me look at the
_subscribefunction and how state is updated through the subject:Assistant
Let me look at
shouldRenderFormState:Assistant
Let me look at the
handleSubmitfunction:Assistant
Let me read
handleSubmitAssistant
Let me look at
getProxyFormState:Assistant
Let me check recent git changes that might be related:
Assistant
Let me search for commits related to the issue
Assistant
Let me look at the commit about reset values with shouldUnregister:
Assistant
Let me look at the changes in that commit:
Assistant
Let me look at
getDirtyFields:Assistant
Let me check how
cloneObjecthandles undefined:Assistant
Now I understand the bug. Let me look at the exact lines that need to change in
_reset:Assistant
Now I have a clear understanding of the bug. Let me look at the full
_resetfunction to implement the fix properly.Assistant
Now I have a clear understanding of the bug. The issue is in the
_resetfunction increateFormControl.ts. WhenshouldUnregisteristrue, aftersetValueis called for each mounted field,_formValuesis overwritten to{}. This means:- Fields register during render →
_formValuesgets values _resetin effects callssetValuefor mounted fields → values are set in_formValues- Then
_formValues = {}clears everything - On re-render, fields don't call
updateValidAndValueagain →_formValuesstays empty handleSubmitclones_formValues→ empty data
The fix: move the
_formValuesassignment to BEFORE thesetValuecalls. Let me implement it.- Fields register during render →
Assistant
Now I'll fix the issue. The problem is that
_formValues = {}happens AFTERsetValuecalls, undoing their work. I need to move the_formValuesassignment before thesetValuecalls.First, let me add
_formValuesassignment before thekeepFieldsRefblock in theelsebranchAssistant
Now I need to add
_formValuesassignment inside thekeepDirtyValuesblock (after the loop) and remove the duplicate from lines 1683-1687.Assistant
Now I need to remove the duplicate
_formValuesassignment at lines 1690-1694. Let me also verify the current state of the file:Assistant
I see there's a duplication. Let me remove the duplicate
_subjects.array.nextand_subjects.state.nextblocks:Assistant
I see a duplicate
_names = {on line 1700. Let me fix itAssistant
Let me verify the full
_resetfunction structure is correctAssistant
I see there's an indentation issue with braces. Let me check and fix it. Let me look at the exact area:
Assistant
I need to remove the extra
}on line 1644. Let me fix theAssistant
There's a duplicate line. Let me fix it:
Assistant
The code structure looks correct now. Let me run the existing tests to make sure nothing is broken
Assistant
Let me check the package.json for test commands
Assistant
Let me look for test-related commands or config.
Assistant
Let me run the tests properly
Assistant
All reset tests pass. Let me run more tests to make sure nothing is broken
Assistant
All tests pass. Let me run the remaining relevant tests
Assistant
All tests pass. Let me run the full test suite to make sure nothing is
Assistant
All 1120 tests pass. Let me now create a reproduction script to verify the fix resolves the
Assistant
Let me copy the test to the proper location and run it
Assistant
Let me fix the import path and re-run
Assistant
Let me check how other tests import
Assistant
Let me find the import
Assistant
Let me look at how tests use
useFormAssistant
Let me check the full import structure
Assistant
The import is
from '../'. Let me fix my test file.Assistant
The fix works correctly. Let me clean up the temporary test file
Assistant
Let me run the full test suite one final time to confirm everything still passes
Assistant
All 1120 tests pass. Let me verify the changes with git
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.