perf: FIT-1302: Fix non-deterministic React keys causing unnecessary remounts#9268
Open
perf: FIT-1302: Fix non-deterministic React keys causing unnecessary remounts#9268
Conversation
Replace non-deterministic and index-based keys with stable identifiers to improve React reconciliation performance. Critical fixes: - App.jsx: Replace guidGenerator() with stable 'relations-overlay' key (was causing RelationsOverlay to remount on every parent render) - PolygonRegion.jsx: Remove guidGenerator() fallback, use item.id only High-frequency component fixes: - SidePanels.tsx: Use Component.displayName/name instead of index - OutlinerTree.tsx: Use tag.name or tag.type instead of index - Grid.jsx: Use selected.id instead of loop index - TextAreaRegionView.jsx: Use item.id with line index for stability Using array index as key causes React to incorrectly diff when items are added, removed, or reordered. Using guidGenerator() creates a new key every render, forcing complete remount of the component tree.
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## develop #9268 +/- ##
===========================================
- Coverage 66.82% 57.91% -8.92%
===========================================
Files 830 561 -269
Lines 64879 40547 -24332
Branches 10971 11008 +37
===========================================
- Hits 43355 23481 -19874
+ Misses 21520 17062 -4458
Partials 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
✅ Deploy Preview for heartex-docs canceled.
|
✅ Deploy Preview for label-studio-docs-new-theme canceled.
|
✅ Deploy Preview for label-studio-storybook ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for label-studio-playground ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two critical anti-patterns in React key usage:
guidGenerator()as key - Creates a new key on every render, forcing React to completely unmount and remount the component. Found inApp.jsxforRelationsOverlay.Array index as key - Causes incorrect diffing when items are added, removed, or reordered. Found in multiple components.
Solution
Replace non-deterministic keys with stable identifiers:
App.jsxkey={guidGenerator()}key="relations-overlay"Grid.jsxkey={i}key={selected.id}SidePanels.tsxkey={i}key={Component.displayName}OutlinerTree.tsxkey={idx}key={tag.name}PolygonRegion.jsxkey={item.id || guidGenerator()}key={item.id}TextAreaRegionView.jsxkey={idx}key={\${item.id}-line-${idx}`}`Files Changed
web/libs/editor/src/components/App/App.jsxweb/libs/editor/src/components/App/Grid.jsxweb/libs/editor/src/components/SidePanels/SidePanels.tsxweb/libs/editor/src/components/SidePanels/OutlinerPanel/OutlinerTree.tsxweb/libs/editor/src/regions/PolygonRegion.jsxweb/libs/editor/src/tags/control/TextArea/TextAreaRegionView.jsx