-
- Notifications
You must be signed in to change notification settings - Fork 560
fix(react-form): prevent array field re-render when child property changes #1930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(react-form): prevent array field re-render when child property changes #1930
Conversation
…anges Array fields with `mode="array"` were incorrectly re-rendering when a property on any array element was mutated. This was a regression introduced in v1.27.0 by the React Compiler compatibility changes. The root cause was that `reactiveStateValue` subscribed to the entire `state.value`, which changes whenever any child property changes. For array mode, this subscription now only tracks the array length, ensuring re-renders only occur when items are added or removed. - Modified `reactiveStateValue` to use array length selector for array mode - Updated `state.value` getter to return actual value from `fieldApi.state.value` - Removed redundant `useStore` subscription for array mode - Added test case to verify array field doesn't re-render on child changes Fixes TanStack#1925
🦋 Changeset detectedLatest commit: 63f5615 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
crutchcorn left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes perfect sense to me! Thank you for this!
| View your CI Pipeline Execution ↗ for commit 63f5615
☁️ Nx Cloud last updated this comment at |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@ ## main #1930 +/- ## =========================================== - Coverage 90.35% 54.11% -36.25% =========================================== Files 38 18 -20 Lines 1752 231 -1521 Branches 444 34 -410 =========================================== - Hits 1583 125 -1458 + Misses 149 94 -55 + Partials 20 12 -8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
mode="array"incorrectly re-rendering when a property on any array element is mutatedProblem
As reported in #1925, starting from v1.27.0, when working with a
form.Fieldthat usesmode="array", when any child Field under the array Field is changed, the entire array Field re-renders.Root Cause
In the React Compiler fix (#1893), a new
reactiveStateValuesubscription was added:This subscribes to the entire
state.value. For array fields, when any child property changes, the array reference changes, causingreactiveStateValueto change, which triggersextendedFieldApirecreation and component re-render.Solution
Make the
reactiveStateValuesubscription aware ofmode:For
mode="array":reactiveStateValuenow tracks only the array lengthstate.valuegetter returns the actual value fromfieldApi.state.valueTest Plan
should not rerender array field when child field value changesFixes #1925