From beeffb89fef9e31cf3947d38b5be151155ff5143 Mon Sep 17 00:00:00 2001 From: Jeff Case Date: Wed, 3 May 2017 10:27:08 -0700 Subject: [PATCH] Made `getItem` and `getItemCount` required props (VirtualizedList) Summary: The main reason to use **VirtualizedList** is to set the `getItem` and `getItemCount` props, so having default values for these props makes things error prone. * In **VirtualizedList**, changed the `getItem` and `getItemCount` props from optional to required, and removed default values. * Ensured that implementing classes **FlatList** and **SectionVirtualizedList** are always passing these props. * Updated VirtualizedList-test.js accordingly. Reviewed By: sahrens Differential Revision: D4980236 fbshipit-source-id: ad1838931253bc61ff9068c40929f6e9c755b92c --- Libraries/Lists/FlatList.js | 2 -- Libraries/Lists/VirtualizedList.js | 18 ++++++++---------- .../Lists/__tests__/VirtualizedList-test.js | 10 ++++++++++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Libraries/Lists/FlatList.js b/Libraries/Lists/FlatList.js index f24aaf5fbd..28932e5bc1 100644 --- a/Libraries/Lists/FlatList.js +++ b/Libraries/Lists/FlatList.js @@ -178,8 +178,6 @@ type Props = RequiredProps & OptionalProps & VirtualizedLis const defaultProps = { ...VirtualizedList.defaultProps, - getItem: undefined, - getItemCount: undefined, numColumns: 1, }; type DefaultProps = typeof defaultProps; diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index bd955f4b2a..1a9f33438b 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -38,6 +38,14 @@ type RequiredProps = { * getItem, getItemCount, and keyExtractor to handle any type of index-based data. */ data?: any, + /** + * A generic accessor for extracting an item from any sort of data blob. + */ + getItem: (data: any, index: number) => ?Item, + /** + * Determines how many items are in the data blob. + */ + getItemCount: (data: any) => number, }; type OptionalProps = { /** @@ -57,14 +65,6 @@ type OptionalProps = { * `data` prop, stick it here and treat it immutably. */ extraData?: any, - /** - * A generic accessor for extracting an item from any sort of data blob. - */ - getItem: (data: any, index: number) => ?Item, - /** - * Determines how many items are in the data blob. - */ - getItemCount: (data: any) => number, getItemLayout?: (data: any, index: number) => {length: number, offset: number, index: number}, // e.g. height, y horizontal?: ?boolean, @@ -254,8 +254,6 @@ class VirtualizedList extends React.PureComponent { static defaultProps = { disableVirtualization: false, - getItem: (data: any, index: number) => data[index], - getItemCount: (data: any) => data ? data.length : 0, horizontal: false, initialNumToRender: 10, keyExtractor: (item: Item, index: number) => { diff --git a/Libraries/Lists/__tests__/VirtualizedList-test.js b/Libraries/Lists/__tests__/VirtualizedList-test.js index 2f3376498b..8134a0aca4 100644 --- a/Libraries/Lists/__tests__/VirtualizedList-test.js +++ b/Libraries/Lists/__tests__/VirtualizedList-test.js @@ -23,6 +23,8 @@ describe('VirtualizedList', () => { } + getItem={(data, index) => data[index]} + getItemCount={(data) => data.length} /> ); expect(component).toMatchSnapshot(); @@ -33,6 +35,8 @@ describe('VirtualizedList', () => { } + getItem={(data, index) => data[index]} + getItemCount={(data) => data.length} /> ); expect(component).toMatchSnapshot(); @@ -43,6 +47,8 @@ describe('VirtualizedList', () => { } + getItem={(data, index) => data[index]} + getItemCount={(data) => 0} /> ); expect(component).toMatchSnapshot(); @@ -55,6 +61,8 @@ describe('VirtualizedList', () => { ListFooterComponent={() =>