From 624c3f46df003ff1d4e9f763ae31c293a450890d Mon Sep 17 00:00:00 2001 From: ErfanBagheri404 Date: Sat, 15 Aug 2026 13:46:13 +0330 Subject: [PATCH] fix(table-core): invalidate value cache when accessorFn changes Fixes #5363 row_getValue and row_getUniqueValues cache values per columnId but never verify the accessor function is still the same one that produced the cached value. When a column's accessorFn is updated (e.g. driven by external state), stale cached values keep being returned. Track the accessorFn alongside each cached value and treat the cache as stale when the function reference changed. --- .../table-core/src/core/rows/constructRow.ts | 1 + .../src/core/rows/coreRowsFeature.types.ts | 1 + .../src/core/rows/coreRowsFeature.utils.ts | 22 ++++++++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/table-core/src/core/rows/constructRow.ts b/packages/table-core/src/core/rows/constructRow.ts index 9fecbb23ed..5ec6eb7610 100644 --- a/packages/table-core/src/core/rows/constructRow.ts +++ b/packages/table-core/src/core/rows/constructRow.ts @@ -51,6 +51,7 @@ export const constructRow = < row._displayIndexCache = -1 row._uniqueValuesCache = makeObjectMap() row._valuesCache = makeObjectMap() + row._accessorFnsCache = makeObjectMap() row.depth = depth row.id = id row.index = rowIndex diff --git a/packages/table-core/src/core/rows/coreRowsFeature.types.ts b/packages/table-core/src/core/rows/coreRowsFeature.types.ts index 8a38bed8c9..1a9b663f59 100644 --- a/packages/table-core/src/core/rows/coreRowsFeature.types.ts +++ b/packages/table-core/src/core/rows/coreRowsFeature.types.ts @@ -25,6 +25,7 @@ export interface Row_CoreProperties< _displayIndexCache: number _uniqueValuesCache: Record _valuesCache: Record + _accessorFnsCache: Record /** * The depth of the row (if nested or grouped) relative to the root row array. */ diff --git a/packages/table-core/src/core/rows/coreRowsFeature.utils.ts b/packages/table-core/src/core/rows/coreRowsFeature.utils.ts index f37ce79828..ff95d2ac3c 100644 --- a/packages/table-core/src/core/rows/coreRowsFeature.utils.ts +++ b/packages/table-core/src/core/rows/coreRowsFeature.utils.ts @@ -79,16 +79,21 @@ export function row_getValue< TFeatures extends TableFeatures, TData extends RowData, >(row: Row, columnId: string) { - if (hasOwn(row._valuesCache, columnId)) { + const column = row.table.getColumn(columnId) + + if ( + hasOwn(row._valuesCache, columnId) && + hasOwn(row._accessorFnsCache, columnId) && + row._accessorFnsCache[columnId] === column?.accessorFn + ) { return row._valuesCache[columnId] } - const column = row.table.getColumn(columnId) - if (!column?.accessorFn) { return undefined } + row._accessorFnsCache[columnId] = column.accessorFn row._valuesCache[columnId] = column.accessorFn(row.original, row.index) return row._valuesCache[columnId] @@ -109,12 +114,16 @@ export function row_getUniqueValues< TFeatures extends TableFeatures, TData extends RowData, >(row: Row, columnId: string) { - if (hasOwn(row._uniqueValuesCache, columnId)) { + const column = row.table.getColumn(columnId) + + if ( + hasOwn(row._uniqueValuesCache, columnId) && + hasOwn(row._accessorFnsCache, columnId) && + row._accessorFnsCache[columnId] === column?.accessorFn + ) { return row._uniqueValuesCache[columnId] } - const column = row.table.getColumn(columnId) - if (!column?.accessorFn) { return undefined } @@ -124,6 +133,7 @@ export function row_getUniqueValues< return row._uniqueValuesCache[columnId] } + row._accessorFnsCache[columnId] = column.accessorFn row._uniqueValuesCache[columnId] = column.columnDef.getUniqueValues( row.original, row.index,