Permalink
Browse files

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
  • Loading branch information...
mariten authored and facebook-github-bot committed May 3, 2017
1 parent 52e50af commit beeffb89fef9e31cf3947d38b5be151155ff5143
@@ -178,8 +178,6 @@ type Props<ItemT> = RequiredProps<ItemT> & OptionalProps<ItemT> & VirtualizedLis
const defaultProps = {
...VirtualizedList.defaultProps,
getItem: undefined,
getItemCount: undefined,
numColumns: 1,
};
type DefaultProps = typeof defaultProps;
@@ -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<OptionalProps, Props, State> {
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) => {
@@ -23,6 +23,8 @@ describe('VirtualizedList', () => {
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={(data) => data.length}
/>
);
expect(component).toMatchSnapshot();
@@ -33,6 +35,8 @@ describe('VirtualizedList', () => {
<VirtualizedList
data={[]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={(data) => data.length}
/>
);
expect(component).toMatchSnapshot();
@@ -43,6 +47,8 @@ describe('VirtualizedList', () => {
<VirtualizedList
data={undefined}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={(data) => 0}
/>
);
expect(component).toMatchSnapshot();
@@ -55,6 +61,8 @@ describe('VirtualizedList', () => {
ListFooterComponent={() => <footer />}
ListHeaderComponent={() => <header />}
data={new Array(5).fill().map((_, ii) => ({id: String(ii)}))}
getItem={(data, index) => data[index]}
getItemCount={(data) => data.length}
keyExtractor={(item, index) => item.id}
getItemLayout={({index}) => ({length: 50, offset: index * 50})}
refreshing={false}
@@ -87,6 +95,8 @@ describe('VirtualizedList', () => {
infos.push(info);
return <item title={info.item.key} />;
}}
getItem={(data, index) => data[index]}
getItemCount={(data) => data.length}
/>
);
expect(component).toMatchSnapshot();

0 comments on commit beeffb8

Please sign in to comment.