Skip to content
TheChatArchiveThe Chat Archive
fable-5· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

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

  1. User

    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.

  2. Assistant

    Now let me write a reproduction test to confirm the bug:

  3. Assistant

    Bug reproduced — onSubmit is called with {}. Now let me trace the root cause more precisely:

  4. Assistant

    The repro passes. Now run the full test suite to check for regressions:

  5. Assistant

    All 1120 tests pass. Now add a permanent regression test alongside the other values-prop tests:

  6. Assistant

    Full suite once more, plus lint and type checks:

  7. Assistant

    Fixed. Summary of the change:

    Root cause

    Since #12923, the values prop path in useForm calls _reset(props.values, { keepFieldsRef: true }) after the first render (src/useForm.ts:155). Inside _reset, the keepFieldsRef branch correctly applies the new values via setValue() for every mounted field — but immediately afterwards, when shouldUnregister: true, _formValues was unconditionally wiped to {}.

    That wipe is only valid for a full reset, where _fields = {} forces every input to remount and repopulate _formValues through updateValidAndValue during re-registration. With keepFieldsRef the field references are preserved, nothing remounts, so _formValues stayed empty — inputs still displayed their values (the DOM refs were updated), but getValues()/handleSubmit returned {}.

    Fix

    src/logic/createFormControl.ts (in _reset):

    if (!_options.shouldUnregister) {
      _formValues = cloneObject(values) as TFieldValues;
    } else if (!keepStateOptions.keepFieldsRef) {
      // fields are remounted after reset and repopulate _formValues on
      // register, whereas keepFieldsRef keeps values applied via setValue
      _formValues = keepStateOptions.keepDefaultValues
        ? (cloneObject(_defaultValues) as TFieldValues)
        : ({} as TFieldValues);
    }
    

    When shouldUnregister + keepFieldsRef are both set, _formValues is left as populated by the setValue loop — which also preserves shouldUnregister semantics (only mounted fields' values are kept, rather than cloning the whole values object).

    Verification

    • Added two regression tests in src/__tests__/useForm.test.tsx: submitting with values + shouldUnregister: true (the reported repro), and updating the values prop afterwards.
    • Sanity-checked (throwaway test) that unmounted fields are still dropped from submit data with this combination.
    • Full suite: 118 suites / 1122 tests pass; tsc --noEmit clean; eslint shows only 3 pre-existing prettier errors that exist on the baseline too.

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