Skip to content

fix(useController): preserve values when shouldUnregister changes - #13996

Merged
bluebill1049 merged 4 commits into
react-hook-form:masterfrom
wozhenbang2004:patch-1
Nov 7, 2026
Merged

bluebill1049 merged 4 commits into
react-hook-form:masterfrom
wozhenbang2004:patch-1

Conversation

@wozhenbang2004

Copy link
Copy Markdown
Contributor

Proposed Changes

Changing the field-level shouldUnregister option reruns the registration effect and cleans up an input that is still mounted. For example, changing true to false removes its edited value from getValues().

Track the committed option independently of registration. Update default-value initialization when the option changes, while keeping registration and cleanup tied to field identity. This preserves inline defaults after reset() and the previous field policy when name or control changes. Form-level precedence and the field-array action guard stay unchanged. This complements #13956 for the field-level option.

Add 12 cases covering option transitions, form-level overrides, identity changes, and inline defaults. Four mounted-option cases fail against the original implementation.

Validation with Node 22:

  • 133 suites, 1,844 unit tests and 26 snapshots passed.
  • All 90 existing Chromium E2E tests passed with one worker and no retries, using a local browser/server configuration. Test files and assertions are unchanged.
  • pnpm type, pnpm test:type, lint, Prettier, builds, and both API Extractor checks passed. No added lint warnings; the API report is unchanged.

Type of change

  • Bug fix

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes

Keep the field-level unregistration option in a committed ref so changing it does not tear down an input that is still mounted. Preserve the previous field policy when the name or control changes.
Update default-value initialization with the option without unregistering a mounted input. Keep cleanup tied to the previous field identity.
Cover mounted option changes, name and control transitions, and inline default values after reset. Preserve form-level overrides and unmount behavior.
@wozhenbang2004

Copy link
Copy Markdown
Contributor Author

The failing setValueAsyncStrictMode test also reproduces with the original useController. I ran the unchanged test and example app 40 times with 2 workers and no retries for each version. Both the original and this PR passed 39/40, with the same timeout and missing A value (["test","B","C","D"]).

The other five CI checks passed. Could a maintainer rerun the failed E2E job?

@bluebill1049
bluebill1049 merged commit 5d963c6 into react-hook-form:master Nov 7, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants