Skip to content

Commit fa33e76

Browse files
AndariusLuna Wei
authored and
Luna Wei
committed
fix: fix virtualizedList scrollToEnd for 0 items (#36067)
Summary: Fixes #36066 ## Changelog [GENERAL] [FIXED] - VirtualizedList scrollToEnd with no data Pull Request resolved: #36067 Test Plan: Run `yarn test VirtualizedList-test` Reviewed By: jacdebug Differential Revision: D43041763 Pulled By: javache fbshipit-source-id: d4d5e871284708a89bf9911d82e9aa97d7625aca
1 parent 9762016 commit fa33e76

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Libraries/Lists/VirtualizedList.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ export default class VirtualizedList extends StateSafePureComponent<
157157
scrollToEnd(params?: ?{animated?: ?boolean, ...}) {
158158
const animated = params ? params.animated : true;
159159
const veryLast = this.props.getItemCount(this.props.data) - 1;
160+
if (veryLast < 0) {
161+
return;
162+
}
160163
const frame = this.__getFrameMetricsApprox(veryLast, this.props);
161164
const offset = Math.max(
162165
0,

Libraries/Lists/__tests__/VirtualizedList-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ describe('VirtualizedList', () => {
137137
expect(component).toMatchSnapshot();
138138
});
139139

140+
it('scrollToEnd works with null list', () => {
141+
const listRef = React.createRef(null);
142+
ReactTestRenderer.create(
143+
<VirtualizedList
144+
data={undefined}
145+
renderItem={({item}) => <item value={item.key} />}
146+
getItem={(data, index) => data[index]}
147+
getItemCount={data => 0}
148+
ref={listRef}
149+
/>,
150+
);
151+
listRef.current.scrollToEnd();
152+
});
153+
140154
it('renders empty list with empty component', () => {
141155
const component = ReactTestRenderer.create(
142156
<VirtualizedList

0 commit comments

Comments
 (0)