From 98009ad94b92320307f2721ee39dbeb9152c0a58 Mon Sep 17 00:00:00 2001 From: Julien Brayere Date: Mon, 6 Feb 2023 08:05:59 -0800 Subject: [PATCH] fix: fix virtualizedList scrollToEnd for 0 items (#36067) Summary: Fixes https://github.com/facebook/react-native/issues/36066 ## Changelog [GENERAL] [FIXED] - VirtualizedList scrollToEnd with no data Pull Request resolved: https://github.com/facebook/react-native/pull/36067 Test Plan: Run `yarn test VirtualizedList-test` Reviewed By: jacdebug Differential Revision: D43041763 Pulled By: javache fbshipit-source-id: d4d5e871284708a89bf9911d82e9aa97d7625aca --- Libraries/Lists/VirtualizedList.js | 3 +++ Libraries/Lists/__tests__/VirtualizedList-test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index 2fad4637e6c57f..efce2bed49bfcd 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -167,6 +167,9 @@ export default class VirtualizedList extends StateSafePureComponent< scrollToEnd(params?: ?{animated?: ?boolean, ...}) { const animated = params ? params.animated : true; const veryLast = this.props.getItemCount(this.props.data) - 1; + if (veryLast < 0) { + return; + } const frame = this.__getFrameMetricsApprox(veryLast, this.props); const offset = Math.max( 0, diff --git a/Libraries/Lists/__tests__/VirtualizedList-test.js b/Libraries/Lists/__tests__/VirtualizedList-test.js index e05f68ee8708aa..45858695abbe8b 100644 --- a/Libraries/Lists/__tests__/VirtualizedList-test.js +++ b/Libraries/Lists/__tests__/VirtualizedList-test.js @@ -137,6 +137,20 @@ describe('VirtualizedList', () => { expect(component).toMatchSnapshot(); }); + it('scrollToEnd works with null list', () => { + const listRef = React.createRef(null); + ReactTestRenderer.create( + } + getItem={(data, index) => data[index]} + getItemCount={data => 0} + ref={listRef} + />, + ); + listRef.current.scrollToEnd(); + }); + it('renders empty list with empty component', () => { const component = ReactTestRenderer.create(