Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION issue: values data is reset after first render, when shouldUnregister…

  1. User

    ISSUE DESCRIPTION

    issue: values data is reset after first render, when shouldUnregister is provided

    Version Number

    7.71.2

    Codesandbox/Expo snack

    https://codesandbox.io/p/sandbox/blazing-dust-3225h9?file=%2Fsrc%2FApp.js

    Steps to reproduce

    1. Simply submit a form
    2. See error

    Expected behaviour

    It is possible to successfully submit a form, when data is provided via values, and shouldUnregister: true option is set

    What 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. GLM Assistant

    Let me explore the repository structure to understand the codebase.

  3. GLM Assistant

    Let me look at the _reset function and how values is used. Let me also look at the key lines around 155-160.

  4. GLM Assistant

    Now let me understand the issue. The problem is that values data is reset after first render, when shouldUnregister is provided.

    Let me look at how values is used in useForm.

  5. GLM Assistant

    Let me reproduce the issue first. Let me look at the registration logic and how _formValues gets populated.

  6. GLM Assistant

    Let me understand the issue better. Let me look at how getValues works during submit, and how _formValues gets populated when fields are registered.

    Let me look at the register function and the updateValid/_updateFieldArray.

    Actually, let me trace through the scenario:

    1. useForm({ values: { firstName: "test", lastName: "test" }, shouldUnregister: true })
    2. createFormControl: _defaultValues = { firstName: "test", lastName: "test" }, _formValues = {} (because shouldUnregister).
    3. The form renders with inputs registered via register("firstName") and register("lastName").

    Let me look at the register function.

  7. GLM Assistant

    Let me look at the generateWatchOutput and _getWatch more closely, and then reproduce the issue.

  8. GLM Assistant

    Let me look at the existing tests to understand the testing setup and find related tests for values + shouldUnregister.

  9. GLM Assistant

    Let me write a reproduction test based on the codesandbox scenario.

  10. GLM Assistant

    Parse error. Let me check the babel config issue - probably TypeScript type annotation. Let me look at the jest config.

  11. GLM Assistant

    I need to use the jest config. Let me run the test properly:

  12. GLM Assistant

    The import path is wrong. Let me fix it.

  13. GLM Assistant

    I reproduced the issue. The inputs are empty (no value) and submit fails. Let me see the full output to understand what the inputs show.

  14. GLM Assistant

    Let me look at the setValue function:

  15. GLM Assistant

    Let me add some debug logging to understand the flow better.

  16. GLM Assistant

    Let me look at the commit 802b04720 which introduced the keepFieldsRef branch, to understand intent.

  17. GLM Assistant

    Before this commit:

    for (const fieldName of _names.mount) {
      const value = get(values, fieldName, get(_defaultValues, fieldName));
      if (!isUndefined(value)) {
        set(values, fieldName, value);
        setValue(fieldName, get(values, fieldName));
      }
    }
    _formValues = cloneObject(values) as TFieldValues;
    

    After this commit:

    if (keepStateOptions.keepFieldsRef) {
      for (const fieldName of _names.mount) {
        setValue(fieldName, get(values, fieldName));
      }
    } else {
      _fields = {};
    }
    _formValues = _options.shouldUnregister
      ? keepStateOptions.keepDefaultValues
        ? (cloneObject(_defaultValues) as TFieldValues)
        : ({} as TFieldValues)
      : (cloneObject(values) as TFieldValues);
    

    So before this commit, _formValues = cloneObject(values) regardless of shouldUnregister. So _formValues would be the actual values {firstName:'test',...}. This worked.

    After this commit, when shouldUnregister is true, _formValues = {} (unless keepDefaultValues). This breaks the values + shouldUnregister case.

    So the original behavior (before this commit) was: when values is provided, _formValues = cloneObject(values). The shouldUnregister logic of _formValues = {} was newly introduced.

    Now, why was the shouldUnregister logic added? Let me think about the original _formValues init:

    let _formValues = _options.shouldUnregister
      ? ({} as TFieldValues)
      : (cloneObject(_defaultValues) as TFieldValues);
    

    So with shouldUnregister, _formValues starts as {}. The idea is that registered fields populate _formValues from _defaultValues (via updateValidAndValue). When shouldUnregister is true, unregistered fields are removed from _formValues.

    In _reset, with shouldUnregister, the intent is: _formValues should only contain registered (mounted) fields. So after reset, _formValues should contain values for mounted fields only. Setting _formValues = {} would lose all values - but then mounted fields would repopulate from _defaultValues via the ref re-registration on re-render.

    Hmm, but the test shows the values are lost. Let me think.

    Actually, the problem: with keepFieldsRef: true (which is what the values effect uses), the fields are kept, so no re-registration happens. But _formValues is reset to {}, and the setValue loop that was supposed to populate _formValues[name] is overwritten by _formValues = {}.

    So the fix should be: when keepFieldsRef is true, the setValue loop already populates _formValues for mounted fields. Setting _formValues = {} after that loses those values.

    The correct behavior: when shouldUnregister and keepFieldsRef is true, _formValues should retain the values set by the setValue loop (i.e., the mounted field values). When keepFieldsRef is false, _fields = {} and _formValues reset to {} is fine (or to _defaultValues if keepDefaultValues).

    Let me reconsider. The original code before the commit always did _formValues = cloneObject(values). But that had a bug with shouldUnregister where unmounted field values would be retained in _formValues (since cloneObject(values) keeps everything). The new commit tried to fix that by setting _formValues = {} for shouldUnregister, which repopulates from _defaultValues on re-register.

    But with keepFieldsRef: true, there's no re-register, so the values are lost.

    So the fix: when keepFieldsRef is true AND shouldUnregister, we should NOT reset _formValues to {}. Instead, we should build _formValues from the mounted field values.

    Actually, let me think about what the correct _formValues should be after reset with shouldUnregister:

    • Only mounted fields should have values in _formValues.
    • The values come from values (the new formValues passed to reset).

    So ideally, _formValues should be an object containing only the mounted field names mapped to their values from values.

    The setValue loop already does set(_formValues, name, value) for each mounted field. So if we DON'T reset _formValues = {} afterwards, the mounted values are retained. But there could be leftover values from previously-mounted-but-now-unmounted fields.

    Hmm. Let me think about what the cleanest fix is.

    Option A: When keepFieldsRef is true, skip the _formValues reset (keep what setValue loop set, but also clear old unmounted values).

    Option B: Build _formValues fresh from mounted fields when shouldUnregister.

    Let me look at how the _formValues reset logic should behave. Actually let me look at the original pre-commit behavior more carefully. Before the commit, for shouldUnregister with values, _formValues = cloneObject(values). So ALL values were kept (including unmounted). That's the behavior that "worked" for the issue (the issue says version 7.71.2 worked... wait, no, the issue is reporting a BUG in 7.71.2).

    Wait, the issue says version 7.71.2 has the bug. Let me check what version introduced this. The commit 802b04720 is dated July 1, 2025. The current version is 7.76.1. So this is a regression introduced by that commit.

    Let me check: was the bug present before the commit? Before the commit, _formValues = cloneObject(values) so values were retained → submit would work. So the commit introduced the regression.

    Now, the cleanest fix that preserves the shouldUnregister intent (not retaining unmounted field values) while fixing the values+shouldUnregister case:

    When keepFieldsRef is true, we want _formValues to contain the mounted field values. The setValue loop populates them. So we should set _formValues to the values for mounted fields only.

    Actually, let me reconsider the whole _formValues assignment. Let me restructure:

    _formValues = _options.shouldUnregister
      ? keepStateOptions.keepDefaultValues
        ? (cloneObject(_defaultValues) as TFieldValues)
        : ({} as TFieldValues)
      : (cloneObject(values) as TFieldValues);
    

    When keepFieldsRef is true and shouldUnregister is true, this sets _formValues = {}. But the setValue loop set values for mounted fields. Setting to {} wipes them.

    But wait - if we set _formValues = {} BEFORE the setValue loop, then the setValue loop would populate it. But the order is: setValue loop first, THEN _formValues = {}. So we need to either reorder or skip the reset when keepFieldsRef.

    Hmm, but the setValue loop uses _setValue which reads previousValue = get(_formValues, name) and uses _formValues. So if we reset _formValues = {} first then loop, the values would be set into the empty object. That works!

    But there's subtlety: setFieldValue (called by _setValue) sets _formValues[name] AND the DOM ref value. So if we set _formValues = {} first, then loop setValue, both _formValues and DOM get populated.

    So one fix: move the _formValues reset to BEFORE the keepFieldsRef loop. But that changes behavior for the keepDirtyValues branch too... no, the keepDirtyValues branch is separate (the if (keepStateOptions.keepDirtyValues) at the top). The keepFieldsRef/else is in the else (not keepDirtyValues) branch.

    Wait, let me re-read the structure:

    if (!keepStateOptions.keepValues) {
      if (keepStateOptions.keepDirtyValues) {
        // ... keepDirtyValues branch ...
      } else {
        if (isWeb && isUndefined(formValues)) {
          // ... form.reset() DOM ...
        }
    
        if (keepStateOptions.keepFieldsRef) {
          for (const fieldName of _names.mount) {
            setValue(fieldName, get(values, fieldName));
          }
        } else {
          _fields = {};
        }
    
        _formValues = _options.shouldUnregister
          ? keepStateOptions.keepDefaultValues
            ? (cloneObject(_defaultValues) as TFieldValues)
            : ({} as TFieldValues)
          : (cloneObject(values) as TFieldValues);
    
        _subjects.array.next({ values: { ...values } });
        _subjects.state.next({ values: { ...values } as TFieldValues });
      }
      // ... _names reset, _state reset ...
    }
    

    So the keepFieldsRef branch and the _formValues reset are both in the else (not keepDirtyValues) branch.

    The cleanest fix: when keepFieldsRef is true and shouldUnregister, set _formValues = {} BEFORE the loop so the loop populates it. OR, keep the loop's result.

    Actually, simplest: move the _formValues assignment to before the loop, OR make the loop re-set after.

    Let me think about which is cleaner and matches the intent. The intent of _formValues = {} for shouldUnregister is: start with empty form values, then mounted fields populate. So the natural place is BEFORE the setValue loop.

    But for the else branch (_fields = {}), there's no loop, so _formValues reset to {} (or _defaultValues) is the final value. Then on re-render, fields re-register and repopulate _formValues from _defaultValues via updateValidAndValue. So that's fine.

    So the fix: move the _formValues assignment to BEFORE the keepFieldsRef loop. Let me verify this works for all cases:

    Case 1: shouldUnregister=false, keepFieldsRef=true.

    • _formValues = cloneObject(values).
    • Loop: setValue for mounted fields. Since _formValues already has values, setValue would set the same values (no real change). DOM gets updated. Fine. Same as before? Before, _formValues = cloneObject(values) after loop. Now before loop. setValue sets _formValues[name] to same value. Effectively same result. ✓

    Case 2: shouldUnregister=true, keepFieldsRef=true.

    • _formValues = {} (keepDefaultValues false).
    • Loop: setValue for mounted fields → _formValues[name] = values[name], DOM updated. ✓ This fixes the bug!

    Case 3: shouldUnregister=true, keepDefaultValues=true, keepFieldsRef=true.

    • _formValues = cloneObject(_defaultValues).
    • Loop: setValue for mounted fields with values[name]. _formValues already has _defaultValues (which == values if not changed). setValue updates. ✓

    Case 4: keepFieldsRef=false (else branch).

    • _fields = {}.
    • _formValues = shouldUnregister ? ... : cloneObject(values).
    • No loop. Re-render repopulates. Same as before. ✓

    So moving the _formValues assignment before the loop works.

    But wait, there's a subtle issue with setValue and isValueUnchanged. In _setValue, isValueUnchanged = deepEqual(previousValue, cloneValue). If _formValues is already set to values (case 1), then previousValue = values[name], cloneValue = cloneObject(values[name]). If they're deepEqual, !isValueUnchanged is false, so set(_formValues, name, cloneValue) is skipped, but setFieldValue is still called (which updates DOM). And the isWatched notification... Let me check the bottom of _setValue.

  18. GLM Assistant

    Let me look at the test that was modified by the commit to ensure I don't break it.

  19. GLM Assistant

    Let me search for keepFieldsRef tests.

  20. GLM Assistant

    Now let me look at the exact lines to edit in the _reset function:

  21. GLM Assistant

    Wait, I notice the closing brace structure. The _formValues = ... is OUTSIDE the if (keepStateOptions.keepFieldsRef) {...} else {...} but INSIDE the else (not keepDirtyValues) branch and inside if (!keepStateOptions.keepValues).

    Let me re-check the indentation. Lines 1664-1674:

            if (keepStateOptions.keepFieldsRef) {
              for (const fieldName of _names.mount) {
                setValue(
                  fieldName as FieldPath<TFieldValues>,
                  get(values, fieldName),
                );
              }
            } else {
              _fields = {};
            }
          }
    
          _formValues = _options.shouldUnregister
    

    So the if (keepFieldsRef) block is at 8-space indent, inside the else (not keepDirtyValues) block at 6-space? Let me re-check by viewing wider context.

  22. GLM Assistant

    Now I understand the bug. The _formValues = ... assignment (which resets to {} when shouldUnregister is true) runs AFTER the keepFieldsRef loop that populates _formValues via setValue. So the loop's work is wiped out.

    The keepFieldsRef path is used by the values effect in useForm.ts. When shouldUnregister: true, _formValues becomes {}, losing the values.

    The fix: the _formValues reset must happen BEFORE the keepFieldsRef loop (so the loop populates the freshly-reset _formValues), while keeping the keepDirtyValues path unchanged (it needs to read the old _formValues first).

    Let me apply the edit:

  23. GLM Assistant

    No python. Let me use node.

  24. GLM Assistant

    Wait, I removed the shared _formValues assignment which was ALSO used by the keepDirtyValues branch. I need to add the assignment back to the keepDirtyValues branch. Let me check the current state.

  25. GLM Assistant

    I need to add the _formValues assignment back into the keepDirtyValues branch (after its loop). Let me do that.

  26. GLM Assistant

    Both branches now have the _formValues assignment in the correct position. Let me run the reproduction test.

  27. GLM Assistant

    Still failing. Let me check what onSubmit received now.

  28. GLM Assistant

    The values are now correct: {firstName:'test', lastName:'test'}! The submit works now. The only test failure is my assertion about the second argument (the event object). Let me fix the test assertion to confirm.

  29. GLM Assistant

    The fix works. The full test suite (114 suites, 1112 tests) passed. Let me run the most relevant test files explicitly to be thorough, and also test edge cases.

  30. GLM Assistant

    All passing. Let me test more edge cases: changing values over time, and values + shouldUnregister + keepDirtyValues.

  31. GLM Assistant

    All edge cases pass. Let me run the complete test suite (including server tests) one final time to ensure no regressions.

  32. GLM Assistant

    All 119 test suites pass (1123 tests). Now let me remove the temporary repro test file and verify my change passes prettier.

  33. GLM Assistant

    Prettier found issues (likely indentation). Let me run prettier to fix the formatting on my changed section.

  34. GLM Assistant

    Prettier also reformatted some unrelated pre-existing style issues. Let me revert the file and re-apply only my intended change cleanly to keep the diff minimal.

  35. GLM Assistant

    Let me open the file at the relevant section and use the edit tool for a clean change.

  36. GLM Assistant

    Now let me make the edit. First, add the _formValues assignment in the keepDirtyValues branch (after the for loop, line 1644-1645):

  37. GLM Assistant

    Now let me move the _formValues assignment in the else branch to before the keepFieldsRef loop (insert before line 1671), and remove the shared assignment at lines 1683-1687.

  38. GLM Assistant

    The edit went wrong - there's an extra } and a duplicate _subjects.array.next({. The line numbers shifted. Let me revert and redo carefully.

  39. GLM Assistant

    Let me use a node script to do the precise full-block replacement in one step.

  40. GLM Assistant

    The prettier warning may be from pre-existing unrelated style issues. Let me check by running prettier --write and comparing which lines it touches.

  41. GLM Assistant

    Prettier's only formatting changes to my added lines are... actually the diff shows my added lines match exactly what I wrote — prettier did NOT reformat my additions. The 3 prettier changes (lines 1114, 1188, 1525) are pre-existing unrelated style issues. To keep my diff minimal, I'll restore my pre-prettier version (which only contains my change) and leave the unrelated lines untouched.

  42. GLM Assistant

    There's a package.json change too — likely from running npm. Let me check and revert it.

  43. GLM Assistant

    npm added a dependency to package.json. Let me revert package.json and check for package-lock changes.

  44. GLM Assistant

    Let me confirm prettier doesn't flag my added lines specifically by diffing a prettier-formatted copy against my version, restricted to my region.

  45. GLM Assistant

    The npx prettier /tmp/mine.ts used default config (double quotes), not the repo's. The in-repo prettier --check only flagged 3 pre-existing issues (lines 1114, 1188, 1525). Let me confirm those pre-existing issues exist in the original HEAD file too.

  46. GLM Assistant

    Confirmed — the prettier warning pre-exists in HEAD (not caused by my change). My added lines are correctly formatted. Let me view my final diff.

  47. GLM Assistant

    The diff is clean and minimal. Let me re-create the repro test, run it plus the full suite to confirm with the final version, then remove the temp test.

  48. GLM Assistant

    All edge cases pass. Now let me remove the temp test and run the full suite one final time to confirm no regressions.

  49. GLM Assistant

    All 118 suites pass (1120 tests, original count restored). Let me also run the TypeScript type check to make sure the change compiles.

  50. GLM Assistant

    TypeScript compiles cleanly. Let me do a final git status check.

  51. GLM Assistant

    The fix is complete and minimal. Let me summarize.

    Summary

    Root cause

    The issue: when values is provided to useForm together with shouldUnregister: true, the form values were reset to {} after the first render, so submitting the form yielded empty data.

    In useForm.ts, the values prop triggers control._reset(props.values, { keepFieldsRef: true, ... }) (introduced by commit 802b04720). Inside _reset (src/logic/createFormControl.ts), the code was:

    if (keepStateOptions.keepFieldsRef) {
      for (const fieldName of _names.mount) {
        setValue(fieldName, get(values, fieldName));   // populates _formValues[name]
      }
    } else {
      _fields = {};
    }
    
    _formValues = _options.shouldUnregister
      ? keepStateOptions.keepDefaultValues
        ? (cloneObject(_defaultValues) as TFieldValues)
        : ({} as TFieldValues)            // <-- wipes the values just set above
      : (cloneObject(values) as TFieldValues);
    

    When shouldUnregister is true (and keepDefaultValues is false), _formValues was reassigned to {} after the keepFieldsRef loop had populated it via setValue. Since keepFieldsRef: true keeps the field references (no re-registration/repopulation happens on re-render), the values were permanently lost — hence empty inputs and an empty submit payload.

    Fix

    Moved the _formValues reassignment to before the keepFieldsRef loop in the non-keepDirtyValues branch, so the freshly-reset _formValues is then populated with the mounted field values by the setValue loop. The keepDirtyValues branch keeps the assignment after its loop (it must read the old _formValues to detect dirty fields first). The previously-shared trailing assignment was removed and duplicated into each branch with the correct ordering.

    Checklist

    • Reproduced the error: Yes — wrote a test mirroring the Codesandbox (useForm({ values, shouldUnregister: true }) + submit) and confirmed it failed with {} received and empty inputs.
    • Confirmed the fix resolves the error: Yes — after the change, the form submits { firstName: 'test', lastName: 'test' } and inputs render the values.
    • Thought about edge cases: Yes — tested (a) the basic submit case, (b) async/changing values with shouldUnregister, and (c) values + shouldUnregister + resetOptions.keepDirtyValues. All pass. The keepDirtyValues path retains its original behavior (assignment still happens after its loop reads old _formValues); the default-reset path (_fields = {}, re-registration repopulates from _defaultValues) is unchanged.
    • Ran existing tests: Yes — all 118 test suites / 1120 tests pass (web + server), and tsc --noEmit is clean. The pre-existing prettier warnings in this file are unrelated to my change (present on HEAD) and left untouched to keep the diff minimal.
  52. GLM Assistant

    The user is telling me no tool call was invoked. I need to submit my changes now since I've completed the fix. Let me run the submit command.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.