Skip to content

🎥 feat: make useWatch and useController to react to name change#13070

Merged
bluebill1049 merged 11 commits intoreact-hook-form:masterfrom
aspirisen:master
Oct 10, 2025
Merged

🎥 feat: make useWatch and useController to react to name change#13070
bluebill1049 merged 11 commits intoreact-hook-form:masterfrom
aspirisen:master

Conversation

@aspirisen
Copy link
Copy Markdown
Contributor

Make useController and useWatch to react to name and control change

Closes: #12974
Related: #13009

@bluebill1049
Copy link
Copy Markdown
Member

e2e test breaks

@aspirisen
Copy link
Copy Markdown
Contributor Author

@bluebill1049 this looks like an CI issue
image

@bluebill1049
Copy link
Copy Markdown
Member

Have you tried locally? I don't see this issue on other prs.

@aspirisen
Copy link
Copy Markdown
Contributor Author

Locally they work
image
Looks like on CI the tests are not event started, some problem with cypress installation

@bluebill1049
Copy link
Copy Markdown
Member

interesting i will check over the weekend thanks @aspirisen for the pr!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR makes the useController and useWatch hooks react to changes in their name and control props, addressing a limitation where these hooks would not update when these key properties changed during re-renders.

  • Refactored useWatch to detect and respond to changes in name and control props
  • Added synchronous value updates when props change to ensure immediate consistency
  • Enhanced test coverage with scenarios for both changing field names and control objects

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/useWatch.ts Restructured hook to track prop changes and provide immediate value updates when name or control changes
src/tests/useWatch.test.tsx Added tests verifying reactive behavior for changing field names and control objects
src/tests/useController.test.tsx Added tests verifying useController reacts to changing field names and control objects

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

aspirisen and others added 2 commits October 3, 2025 14:51
…rence before deepEqual and caching computed output to reduce duplicate calls.
src/useWatch.ts Outdated
Comment on lines +351 to +358
// If name or control changed for this render, synchronously reflect the
// latest value so callers (like useController) see the correct value
// immediately on the same render.

// Optimize: Check control reference first before expensive deepEqual
const controlChanged = _prevControl.current !== control;
const nameChanged = !controlChanged && !deepEqual(_prevName.current, name);
const shouldReturnImmediate = controlChanged || nameChanged;
Copy link
Copy Markdown
Member

@bluebill1049 bluebill1049 Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can bring this as part of the following memo, probably only need the controlChanged to be left out.

@bluebill1049 bluebill1049 changed the title Make useWatch and useController to react to passed name change 🎥 feat: make useWatch and useController to react to name change Oct 4, 2025
…g control changes first and optimizing memoization for computed output.
…nsuring immediate return on control and name changes while simplifying the conditions for output calculation.
@bluebill1049 bluebill1049 merged commit 0b8be38 into react-hook-form:master Oct 10, 2025
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.

issue: useController change name not trigger update value

3 participants