From 7717d194a2550c6299cebdf5a38cbc4e96203d7c Mon Sep 17 00:00:00 2001 From: Richie Date: Sat, 25 Jan 2025 06:21:10 +0100 Subject: [PATCH] Avoid invalidating getIndexes memo cache on every call calculateRange() returns a new object busting the memo cache on every call to getIndexes(). Passing the scalar startIndex and endIndex from calculateRange() fixes this issue. --- packages/virtual-core/src/index.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index c35101b11..0519854d8 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -697,18 +697,28 @@ export class Virtualizer< ) private getIndexes = memo( - () => [ - this.options.rangeExtractor, - this.calculateRange(), - this.options.overscan, - this.options.count, - ], - (rangeExtractor, range, overscan, count) => { - return range === null + () => { + let startIndex: number | null = null + let endIndex: number | null = null + const range = this.calculateRange() + if (range) { + startIndex = range.startIndex + endIndex = range.endIndex + } + return [ + this.options.rangeExtractor, + this.options.overscan, + this.options.count, + startIndex, + endIndex, + ] + }, + (rangeExtractor, overscan, count, startIndex, endIndex) => { + return startIndex === null || endIndex === null ? [] : rangeExtractor({ - startIndex: range.startIndex, - endIndex: range.endIndex, + startIndex, + endIndex, overscan, count, })