From e0a509d462c1eaf7b05ea329ec96da8b22384e34 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 1 Jul 2021 02:51:11 -0400 Subject: [PATCH] Always check if ref is active in virtual-list (#3230) Signed-off-by: Sebastian Malton --- .../components/virtual-list/virtual-list.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/renderer/components/virtual-list/virtual-list.tsx b/src/renderer/components/virtual-list/virtual-list.tsx index c5a32edc43..ad6d93af3a 100644 --- a/src/renderer/components/virtual-list/virtual-list.tsx +++ b/src/renderer/components/virtual-list/virtual-list.tsx @@ -75,23 +75,27 @@ export class VirtualList extends Component { const { items, rowHeights } = this.props; if (prevProps.items.length !== items.length || !isEqual(prevProps.rowHeights, rowHeights)) { - this.listRef.current.resetAfterIndex(0, false); + this.listRef.current?.resetAfterIndex(0, false); } } getItemSize = (index: number) => this.props.rowHeights[index]; scrollToSelectedItem = debounce(() => { - if (!this.props.selectedItemId) return; + if (!this.props.selectedItemId) { + return; + } + const { items, selectedItemId } = this.props; const index = items.findIndex(item => item.getId() == selectedItemId); - if (index === -1) return; - this.listRef.current.scrollToItem(index, "start"); + if (index >= 0) { + this.listRef.current?.scrollToItem(index, "start"); + } }); scrollToItem = (index: number, align: Align) => { - this.listRef.current.scrollToItem(index, align); + this.listRef.current?.scrollToItem(index, align); }; render() {