fix(react-form,preact-form): prevent unnecessary FormGroup child re-renders - #2380
fix(react-form,preact-form): prevent unnecessary FormGroup child re-renders#2380dikshit-n wants to merge 1 commit into
Conversation
…enders (TanStack#2377) Subscribe to the form's baseStore with a path-based selector instead of the FormGroup's own store value. This ensures useFormGroup only re-renders when a field *inside this specific group* changes, not when any sibling group's value changes. Issue: changing a field value in a FormGroup caused ALL child components in the group to re-render, even when those components didn't read the changed field.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthrough
ChangesFormGroup rendering optimization
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to The FormGroup subscription change is covered for sibling-group isolation in React and applies the same path-based selector in both adapters. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Full details: Linked Issues checkExplanation The changes address unnecessary re-renders caused by fields in sibling FormGroups [ Resolution Update the FormGroup subscription or child field subscription model so a field change does not re-render unrelated children within the same FormGroup. Add a regression test that changes one field and verifies unrelated fields in the same FormGroup do not re-render. [
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
When a field value inside a
FormGroupchanges, all child components in thatFormGroupwere re-rendering — even components that had no dependency on the changed field. This is a significant performance regression for forms with many fields in a group.Root Cause
useFormGroupsubscribed to the FormGroup's own store with the selector(state) => state.value. This selector returns the entire group value object, which gets a new reference every time any field in the group changes. Because the selector fires on every group-level store update, theuseMemothat constructsextendedFieldApialways recomputes, causing a full re-render of all children.Fix
Subscribe to the form's
baseStorewith a path-based selector that reads only the specific group value atopts.name. UsinggetBy(state.values, opts.name)means:This mirrors the existing optimization pattern used for array-mode fields in
useField.Changes
useSelectorto read fromformGroupApi.form.baseStorewith agetBy(state.values, opts.name)selectorTesting
A new unit test (
should not re-render sibling field components when a field value changes) verifies that changing a field instep2does not trigger a re-render of a field inside thestep1FormGroup.Closes #2377
Summary by CodeRabbit
Performance Improvements
Bug Fixes
Tests