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
This commit is contained in:
Julien Brayere 2023-02-06 08:05:59 -08:00 коммит произвёл Facebook GitHub Bot
Родитель 34604fa8fd
Коммит 98009ad94b
2 изменённых файлов: 17 добавлений и 0 удалений

Просмотреть файл

@ -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,

Просмотреть файл

@ -137,6 +137,20 @@ describe('VirtualizedList', () => {
expect(component).toMatchSnapshot();
});
it('scrollToEnd works with null list', () => {
const listRef = React.createRef(null);
ReactTestRenderer.create(
<VirtualizedList
data={undefined}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => 0}
ref={listRef}
/>,
);
listRef.current.scrollToEnd();
});
it('renders empty list with empty component', () => {
const component = ReactTestRenderer.create(
<VirtualizedList