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 what we're working with.
Assistant
Let me look at the codesandbox example to understand the issue:
Assistant
Let me look at the
createFormControl.tsto understand the reset behavior:Assistant
Now I need to look at the
useForm.tsmore carefully. Let me also look at how the values get reset:Assistant
Now I understand the issue better. Let me trace through:
- On first render,
useFormis called withvalues: { firstName: 'bill', lastName: 'luo' }andshouldUnregister: true. _formValuesis initially{}(becauseshouldUnregister: true).- The first
useEffectruns:props.shouldUnregister && control._subjects.state.next({ values: control._getWatch() }). This will use since is .
- On first render,
Assistant
So
getValuesreturns_formValueswhen_state.mountistrue. The_state.mountis set inside_reset:javascript_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid || !!keepStateOptions.keepDirtyValues || (!_options.shouldUnregister && !isEmptyObject(values));When
_resetis called with the values fromuseForm,keepStateOptionsis{ keepFieldsRef: true }. So:!_proxyFormState.isValid- depends on initial statekeepIsValid- falsekeepDirtyValues- false!_options.shouldUnregister && !isEmptyObject(values)- false becauseshouldUnregisteris true
So
_state.mountbecomesfalseafter the first_resetcall.Now after this, the inputs are registered, which calls
setFieldValuefor each field, which callsset(_formValues, name, ...).Wait but this happens only on input change. Let melook at register:
Assistant
Let me look at
setValueto see how it works:Assistant
Let me look at the codesandbox URL content:
Assistant
Let me first try to set up a test environment to actually reproduce the issue:
Assistant
Now let me create a simple test to reproduce the issue:
Assistant
I've reproduced the issue. The form submission didn't happen. Let me check why by updating the test:
Assistant
Let me debug this more carefully:
Assistant
Now I can confirm the bug. The
getValues: {}is empty after render. Let me check the issue more carefully. The problem is in_reset:Assistant
Let me check the
_removeUnmountedfunction:Assistant
Assistant
Let me check the existing tests for
shouldUnregister:Assistant
OK so
keepFieldsRefis only used inuseForm.tsfor thevalueseffect. Let me now check the test fileforresetwithshouldUnregister:Assistant
Now let me look at how the fix should work. Let me see other places where
_resetis called and the existing behavior ofshouldUnregister:Assistant
Now I have a clear understanding. Let me look at the
_resetfunction once more:Assistant
Now let me apply the fix:
Assistant
Oh no, my edit accidentally removed the
_subjects.array.nextblock. Let me check and fix:Assistant
I see - the
_subjects.array.nextblock was lost. Let me revert and re-do the edit properly:Assistant
Let me revert and try again:
Assistant
Now let me apply the fix correctly using
replace:Assistant
Now let me run my test to confirm the fix works:
Assistant
Still failing. Let me add more debug output:
Assistant
getValuesnow returns the correct values, but the form submission isn't happening. Let me look athandleSubmit:Assistant
Let me check if there's an issue with the async behavior of
handleSubmit:Assistant
The fix works. Let me now check edge cases. Let me think about what could break:
Assistant
Both tests pass. Let me also run the existing tests to make sure nothing is broken:
Assistant
All reset tests pass. Now let me run more tests to be sure:
Assistant
All tests pass. Let me also run the entire test suite to be sure:
Assistant
All 1113 tests pass. Now let me also clean up my test file:
Assistant
Let me verify the changes I've made are exactly what I intended:
Assistant
I see there's an unintended change to package.json. Let me revert it:
Assistant
Now the only change is the fix to the
_resetfunction. Let me also check if there are any other places where this could be a problem. Let me look at theuseEffectfor values inuseForm.ts:Assistant
Let me run more comprehensive tests to make sure I haven't broken anything:
Assistant
All 1111 tests pass (the difference between 1113 and 1111 is because I removed my 2 tests).
Let me also test some edge cases that involve my fix more thoroughly:
Assistant
Let me check if this test was passing before my fix:
Assistant
So before my fix, 3 tests were failing. With my fix, only 1 test is failing. Let me restore my fix:
Assistant
Now let me check the failing test more carefully. The issue is the test doesn't wait for the re-render:
Assistant
All my edge case tests pass. Let me clean up:
Assistant
Let me run the entire test suite once more to confirm:
Assistant
All 1111 tests pass. Now let me look at the changes one more time to ensure the fix is correct:
Assistant
Let me also think about whether there's a regression with the existing tests for setValues, etc:
Assistant
All tests pass. Let me verify the final fix once more:
Assistant
Let me also check the test for setting values with shouldUnregister and the watch test:
Assistant
Let me also check the related test in
setValue.test.tsx:Assistant
Let me also check the formState test to be sure:
Assistant
Let me verify the fix by running the entire test suite once more:
Assistant
All 1111 tests pass. Let me also verify the diff:
Assistant
Now let me also check if I should consider the scenario where the issue happens with
defaultValuesprovided as a function (because of_resetDefaultValues):Assistant
Let me check the docs for more context:
Assistant
Let me also check if there's any documentation:
Assistant
Let me also verify my fix handles the scenario in the original issue with the codesandbox URL by manually tracing through:
Assistant
The fix works. Let me clean up:
Assistant
Now let me do the final check:
Assistant
Let me do one more comprehensive test:
Assistant
All 1111 tests pass. Let me also verify the final fix one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.