Skip to content
Source
Sign inSign up
TanStack/table

fix(table-core): skip state update in toggleExpanded when state is unchanged #6184

Closedisaackaara(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/6184/headopened

Showing 2 changed files with 17 additions and 11 deletions.

x
0 of 2 viewed, 2 left
5 additions and 0 deletions.changeset/fix-toggle-expanded-noop.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/table-core': patch
+---
+​
+Skip state update in toggleExpanded when the desired state matches the current state, avoiding unnecessary re-renders
12 additions and 11 deletionspackages/table-core/src/features/RowExpanding.tsModified
View file
@@ -290,9 +290,16 @@
table: Table<TData>,
): void => {
row.toggleExpanded = (expanded) => {
- table.setExpanded((old) => {
- const exists = old === true ? true : !!old?.[row.id]
+ const isCurrentlyExpanded = row.getIsExpanded()
+ const newExpanded = expanded ?? !isCurrentlyExpanded
​
+ // If the desired state matches the current state, skip the update
+ // to avoid unnecessary re-renders
+ if (newExpanded === isCurrentlyExpanded) {
+ return
+ }
+​
+ table.setExpanded((old) => {
let oldExpanded: ExpandedStateList = {}