From 2f78a02db98997a4ea0a07f1a645820eb542fb14 Mon Sep 17 00:00:00 2001 From: Hakan Kosdag Date: Fri, 28 Feb 2025 05:48:17 +0300 Subject: [PATCH 1/2] fix: add mergedData length check --- src/List.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/List.tsx b/src/List.tsx index 9afe3bfb..10a52f49 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -277,7 +277,7 @@ export function RawList(props: ListProps, ref: React.Ref) { // Let's sync scroll top to avoid jump React.useLayoutEffect(() => { const changedRecord = heights.getRecord(); - if (changedRecord.size === 1) { + if (changedRecord.size === 1 && mergedData.length > 0) { const recordKey = Array.from(changedRecord)[0]; const startIndexKey = getKey(mergedData[start]); if (startIndexKey === recordKey) { From eb4f6e5ca313354cb37bf44c0a030a03fab10a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Fri, 28 Feb 2025 14:35:34 +0800 Subject: [PATCH 2/2] chore: check first --- src/List.tsx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/List.tsx b/src/List.tsx index 10a52f49..0da61f32 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -277,15 +277,19 @@ export function RawList(props: ListProps, ref: React.Ref) { // Let's sync scroll top to avoid jump React.useLayoutEffect(() => { const changedRecord = heights.getRecord(); - if (changedRecord.size === 1 && mergedData.length > 0) { + if (changedRecord.size === 1) { const recordKey = Array.from(changedRecord)[0]; - const startIndexKey = getKey(mergedData[start]); - if (startIndexKey === recordKey) { - const realStartHeight = heights.get(recordKey); - const diffHeight = realStartHeight - itemHeight; - syncScrollTop((ori) => { - return ori + diffHeight; - }); + // Quick switch data may cause `start` not in `mergedData` anymore + const startItem = mergedData[start]; + if (startItem) { + const startIndexKey = getKey(startItem); + if (startIndexKey === recordKey) { + const realStartHeight = heights.get(recordKey); + const diffHeight = realStartHeight - itemHeight; + syncScrollTop((ori) => { + return ori + diffHeight; + }); + } } }