From ea707b2c08fc8d04e68272d77b46d294a9a7a13d Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Fri, 15 Nov 2019 18:04:30 +0300 Subject: [PATCH] Fix for wrong ref call order --- DynamicVirtualScroll.js | 6 ++++-- VirtualScrollList.js | 14 +++++++++++++- package.json | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/DynamicVirtualScroll.js b/DynamicVirtualScroll.js index 07514f8..62e2606 100644 --- a/DynamicVirtualScroll.js +++ b/DynamicVirtualScroll.js @@ -61,8 +61,10 @@ export function virtualScrollDriver(props, oldState, getRenderedItemHeight) lastItemSize = getRenderedItemHeight(props.totalItems - 1 - lastVisibleItems); if (!lastItemSize) { - // Some required items in the end are missing - return newState; + // Some required items in the end are missing, complain about it loudly, but don't break scrolling + // eslint-disable-next-line + console.error('dynamic-virtual-scroll: item #'+(props.totalItems - 1 - lastVisibleItems)+' is missing from render, please fix your code'); + lastItemSize = 0; } lastItemsHeight += lastItemSize < props.minRowHeight ? props.minRowHeight : lastItemSize; lastVisibleItems++; diff --git a/VirtualScrollList.js b/VirtualScrollList.js index f0eab70..45ffbd5 100644 --- a/VirtualScrollList.js +++ b/VirtualScrollList.js @@ -36,10 +36,22 @@ export class VirtualScrollList extends React.Component setItemRef = [] itemRefs = [] + itemRefCount = [] makeRef(i) { - this.setItemRef[i] = (e) => this.itemRefs[i] = e; + this.setItemRef[i] = (e) => + { + // If the new row instance is mounted before unmouting the old one, + // we get called 2 times in wrong order: first with the new instance, + // then with null telling us that the old one is unmounted. + // We track reference count to workaround it. + this.itemRefCount[i] = (this.itemRefCount[i]||0) + (e ? 1 : -1); + if (e || !this.itemRefCount[i]) + { + this.itemRefs[i] = e; + } + }; } renderItems(start, count, is_end) diff --git a/package.json b/package.json index 140388c..1123d1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dynamic-virtual-scroll", - "version": "1.0.13", + "version": "1.0.14", "author": { "name": "Vitaliy Filippov", "email": "vitalif@yourcmc.ru",