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

    I'll start by exploring the repository structure to understand the codebase.

  3. GLM Assistant

    I need to make only one tool call at a time. Let me explore the repository structure.

  4. GLM Assistant

    The main logic file is createFormControl.ts. Let me look at it, particularly focusing on the values and shouldUnregister handling.

  5. GLM Assistant

    Let me look at the reset function more closely, which is likely where the issue lies.

  6. GLM Assistant

    Now let me look at the reset function (the public one) and how it's invoked.

  7. GLM Assistant

    Now let me look at the useForm.ts to understand how values and defaultValues are passed and how reset is called on mount.

  8. GLM Assistant

    Let me look at the setValue function and the register logic.

  9. GLM Assistant

    Let me understand the flow better. Let me look at the _reset function again with the keepFieldsRef logic.

    The issue: When shouldUnregister: true and values is provided:

    1. useEffect calls control._reset(props.values, { keepFieldsRef: true, ...resetOptions })
    2. In _reset:
      • _defaultValues = updatedValues (props.values)
      • keepFieldsRef is true, so for each mounted field, setValue is called with the value from values
      • _formValues = shouldUnregister ? (keepDefaultValues ? cloneObject(_defaultValues) : {}) : cloneObject(values)
      • Since shouldUnregister is true and keepDefaultValues is false: _formValues = {}

    So after the reset, _formValues is {}, even though we just set values for mounted fields via setValue.

    Wait, but setValue calls setFieldValue which calls set(_formValues, name, ...). So setting _formValues = {} AFTER the setValue loop would wipe out the values that were just set!

    Let me re-read the code:

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

    Then AFTER this if/else block:

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

    So yes, after the keepFieldsRef loop calls setValue (which populates _formValues), _formValues is then set to {} when shouldUnregister is true and keepDefaultValues is false. This wipes out the values!

    So the fix should be: when keepFieldsRef is true (which is set when values is provided in the useEffect), we should NOT reset _formValues to {}.

    Let me look at the git blame / history to understand when this was changed.

    Actually, let me think about what the correct behavior should be:

    • When shouldUnregister is true, _formValues should only contain registered (mounted) fields.
    • When we reset with values, the keepFieldsRef loop calls setValue for each mounted field, which populates _formValues with the values for those mounted fields.
    • So after the keepFieldsRef loop, _formValues already has the correct values for the mounted fields.
    • Setting _formValues = {} after that would wipe them out, which is the bug.

    So the fix: when keepFieldsRef is true, we should keep the _formValues that were just populated by the setValue loop, OR set _formValues to the cloned values.

    Wait, but actually, the issue is that when shouldUnregister is true, the intent of _formValues = {} is to start fresh (only registered fields should be in _formValues). But the keepFieldsRef loop already handles populating _formValues via setValue.

    Let me check: does setValue actually populate _formValues? Let me look at the setValue function.

  10. GLM Assistant

    Let me look at _setValue.

  11. GLM Assistant

    Let me first reproduce the issue. Let me look at the test structure.

  12. GLM Assistant

    Let me look at existing tests that use values to understand the test format.

  13. GLM Assistant

    Now let me create a reproduction test to confirm the issue.

  14. GLM Assistant

    The cast syntax is causing a parse issue. Let me fix the test.

  15. GLM Assistant

    The babel config doesn't support TypeScript as casts in this context. Let me check the babel config.

  16. GLM Assistant

    The transform uses @swc/jest but the error shows babel. Let me just avoid type casts in my test.

  17. GLM Assistant

    The transform isn't being applied. Let me run an existing test to verify the setup works.

  18. GLM Assistant

    I need to use the --config flag. Let me run my repro test with the proper config.

  19. GLM Assistant

    The simple test passes. Let me look at the actual scenario more carefully. Let me check the handleSubmit flow and how form values are extracted on submit.

  20. GLM Assistant

    Let me look at the register function to understand how fields get their values.

  21. GLM Assistant

    Let me look at updateValidAndValue.

  22. GLM Assistant

    I see - the issue is about timing. In a real component, fields register during render (before the values useEffect runs). Let me write a component-based reproduction test.

  23. GLM Assistant

    I've reproduced the issue. The submitted data is {} instead of { test: 'data' }. Now let me understand the root cause. Let me look at the _reset function and its _formValues assignment.

  24. GLM Assistant

    Now I understand the change. Before commit 802b0472, _formValues was simply cloneObject(values). The commit changed it to wipe _formValues to {} when shouldUnregister is true and keepDefaultValues is false. Let me check the test that was added with this commit.

  25. GLM Assistant

    Let me look at that test in full context to understand what scenario it covers.

  26. GLM Assistant

    Let me look at the isEmptyObject utility and the _reset function structure once more.

  27. GLM Assistant

    OK so isEmptyObject(undefined) = isObject(undefined) && ... = false && ... = false. So isEmpty = false.

    So for setValue(fieldName, undefined):

    • isEmpty = false.
    • !field || field._f || isNullOrUndefined(cloneValue) || isEmpty.
    • isNullOrUndefined(undefined) = true. So this condition is true.
    • setFieldValue(fieldName, undefined, {}, false).

    setFieldValue:

    • field = get(_fields, name).
    • fieldReference = field._f.
    • !fieldReference.disabled && set(_formValues, name, getFieldValueAs(undefined, fieldReference)).
    • getFieldValueAs(undefined, fieldReference) - let me check.

    Let me look at getFieldValueAs.

  28. GLM Assistant

    Let me view the exact lines of the _formValues assignment in _reset.

  29. GLM Assistant

    Let me view the full _reset structure around the keepFieldsRef logic to understand the nesting.

  30. GLM Assistant

    Now I understand the issue clearly. The keepFieldsRef loop populates _formValues via setValue, but then the _formValues assignment wipes it to {} when shouldUnregister is true and keepDefaultValues is false. This was introduced by commit 802b0472.

    Let me make the fix. When keepFieldsRef is true (the values prop case), the loop has already populated _formValues with the mounted field values, so we should keep them instead of wiping to {}.

  31. GLM Assistant

    Now let me apply the fix:

  32. GLM Assistant

    The fix is applied. Now let me run my reproduction test to confirm the fix works.

  33. GLM Assistant

    The fix works - the submitted data is now { test: 'data' } instead of {}. Let me think about edge cases and test a few more scenarios. Let me extend my reproduction test to cover edge cases.

  34. GLM Assistant

    All edge cases pass. Now let me run the existing test suites to make sure I didn't break anything. Let me start with the most relevant ones.

  35. GLM Assistant

    All pass (the trace shown was just a waitFor retry log, tests all passed). Let me run the broader test suite including useFieldArray, useController, useWatch, and unregister tests since shouldUnregister is related.

  36. GLM Assistant

    All pass (the trace is just a React act warning, not a failure). Let me run the full useForm test directory plus field array tests to be thorough.

  37. GLM Assistant

    All 489 tests pass. Let me run the entire test suite to be extra safe.

  38. GLM Assistant

    All 1124 tests pass across 119 test suites (both Web and Server projects). Now let me clean up my reproduction test file before submitting.

  39. GLM Assistant

    I see package.json is also modified, which I didn't intend. Let me check the diff.

  40. GLM Assistant

    This package.json change is unrelated to my fix (likely a pre-existing environment artifact). Let me revert it to keep my submission clean.

  41. GLM Assistant

    The diff is now clean - only the relevant change. Let me do a final verification by recreating and running the reproduction test, then cleaning up.

  42. GLM Assistant

    The fix works and the reproduction test is cleaned up. Let me verify the final state of the repository.

  43. GLM Assistant

    The change is clean and minimal. Let me review the final fix once more in context:

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