Skip to content

fix: sync Controller value from _formValues on render to handle Activ…#13382

Open
ISHANTPATIL2005 wants to merge 1 commit into
react-hook-form:masterfrom
ISHANTPATIL2005:fix/controller-stale-value-in-activity
Open

fix: sync Controller value from _formValues on render to handle Activ…#13382
ISHANTPATIL2005 wants to merge 1 commit into
react-hook-form:masterfrom
ISHANTPATIL2005:fix/controller-stale-value-in-activity

Conversation

@ISHANTPATIL2005
Copy link
Copy Markdown

Fixes #13381

Problem

When Controller is inside a React Native Activity (or any tab layout
that keeps components mounted but hidden), it shows a stale value when
the tab becomes visible again.

Steps to reproduce are in the issue with a CodeSandbox link:
https://codesandbox.io/p/sandbox/controller-activity-bug-6tpzdl

Why

useController uses useWatch internally. When a component is hidden
inside an Activity, React skips state updates to that subtree. So
useWatch misses the update and holds the old value.

Fix

control._formValues always has the latest value regardless of React's
render cycle. On each render, if _formValues and useWatch disagree,
we use _formValues instead. No extra re-renders.

Changes

  • src/useController.ts — sync value from _formValues on render
  • src/__tests__/useController.test.tsx — added regression test

Code of Conduct

  • I agree to follow this project's Code of Conduct

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: Controller does not update value as child of Activity

1 participant