From 60b2ddf825a4c32302003c47bedbabebc332f4b4 Mon Sep 17 00:00:00 2001 From: Isaac Hunja Date: Wed, 4 Mar 2026 06:53:47 +0300 Subject: [PATCH 1/2] fix(table-core): skip state update in toggleExpanded when state is unchanged When calling row.toggleExpanded(bool) with a value matching the current expanded state, the function would still trigger onExpandedChange and cause unnecessary re-renders. This adds an early return that checks the current expanded state before proceeding with the state update, avoiding the overhead of calling onExpandedChange when no state change is needed. Closes #6136 --- .../table-core/src/features/RowExpanding.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/table-core/src/features/RowExpanding.ts b/packages/table-core/src/features/RowExpanding.ts index 456108d4d4..fb190768d0 100644 --- a/packages/table-core/src/features/RowExpanding.ts +++ b/packages/table-core/src/features/RowExpanding.ts @@ -290,9 +290,16 @@ export const RowExpanding: TableFeature = { table: Table, ): 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 = {} if (old === true) { @@ -303,21 +310,15 @@ export const RowExpanding: TableFeature = { oldExpanded = old } - expanded = expanded ?? !exists - - if (!exists && expanded) { + if (newExpanded) { return { ...oldExpanded, [row.id]: true, } } - if (exists && !expanded) { - const { [row.id]: _, ...rest } = oldExpanded - return rest - } - - return old + const { [row.id]: _, ...rest } = oldExpanded + return rest }) } row.getIsExpanded = () => { From 6cd1aa963348dcc181239b90ba8f370a11954552 Mon Sep 17 00:00:00 2001 From: Isaac Hunja Date: Wed, 4 Mar 2026 07:10:17 +0300 Subject: [PATCH 2/2] chore: add changeset --- .changeset/fix-toggle-expanded-noop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fix-toggle-expanded-noop.md diff --git a/.changeset/fix-toggle-expanded-noop.md b/.changeset/fix-toggle-expanded-noop.md new file mode 100644 index 0000000000..360084523d --- /dev/null +++ b/.changeset/fix-toggle-expanded-noop.md @@ -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