@@ -41,22 +41,23 @@ const invariant = require('invariant');
4141
4242import type { StyleObj } from 'StyleSheetTypes' ;
4343import type { Viewable } from 'ViewabilityHelper' ;
44+ import type { Props as VirtualizedListProps } from 'VirtualizedList' ;
4445
4546type Item = any ;
4647
47- type RequiredProps = {
48+ type RequiredProps < ItemT > = {
4849 /**
4950 * Note this can be a normal class component, or a functional component, such as a render method
5051 * on your main component.
5152 */
52- ItemComponent : ReactClass < { item : Item , index : number } > ,
53+ ItemComponent : ReactClass < { item : ItemT , index : number } > ,
5354 /**
5455 * For simplicity, data is just a plain array. If you want to use something else, like an
5556 * immutable list, use the underlying `VirtualizedList` directly.
5657 */
57- data : ?Array < Item > ,
58+ data : ?Array < ItemT > ,
5859} ;
59- type OptionalProps = {
60+ type OptionalProps < ItemT > = {
6061 /**
6162 * Rendered at the bottom of all the items.
6263 */
@@ -79,7 +80,7 @@ type OptionalProps = {
7980 * Remember to include separator length (height or width) in your offset calculation if you
8081 * specify `SeparatorComponent`.
8182 */
82- getItemLayout ?: ( data : ?Array < Item > , index : number ) =>
83+ getItemLayout ?: ( data : ?Array < ItemT > , index : number ) =>
8384 { length : number , offset : number , index : number } ,
8485 /**
8586 * If true, renders items next to each other horizontally instead of stacked vertically.
@@ -90,7 +91,7 @@ type OptionalProps = {
9091 * and as the react key to track item re-ordering. The default extractor checks item.key, then
9192 * falls back to using the index, like react does.
9293 */
93- keyExtractor : ( item : Item , index : number ) => string ,
94+ keyExtractor : ( item : ItemT , index : number ) => string ,
9495 /**
9596 * Multiple columns can only be rendered with horizontal={false} and will zig-zag like a flexWrap
9697 * layout. Items should all be the same height - masonry layouts are not supported.
@@ -111,6 +112,7 @@ type OptionalProps = {
111112 * `viewablePercentThreshold` prop.
112113 */
113114 onViewableItemsChanged ?: ?( { viewableItems : Array < Viewable > , changed : Array < Viewable > } ) => void ,
115+ legacyImplementation ?: ?boolean ,
114116 /**
115117 * Set this true while waiting for new data from a refresh.
116118 */
@@ -123,11 +125,19 @@ type OptionalProps = {
123125 * Optional optimization to minimize re-rendering items.
124126 */
125127 shouldItemUpdate : (
126- prevProps : { item : Item , index : number } ,
127- nextProps : { item : Item , index : number }
128+ prevProps : { item : ItemT , index : number } ,
129+ nextProps : { item : ItemT , index : number }
128130 ) => boolean ,
129131} ;
130- type Props = RequiredProps & OptionalProps ; // plus props from the underlying implementation
132+ type Props < ItemT > = RequiredProps < ItemT > & OptionalProps < ItemT > & VirtualizedListProps ;
133+
134+ const defaultProps = {
135+ ...VirtualizedList . defaultProps ,
136+ getItem : undefined ,
137+ getItemCount : undefined ,
138+ numColumns : 1 ,
139+ } ;
140+ type DefaultProps = typeof defaultProps ;
131141
132142/**
133143 * A performant interface for rendering simple, flat lists, supporting the most handy features:
@@ -148,13 +158,9 @@ type Props = RequiredProps & OptionalProps; // plus props from the underlying im
148158 * ItemComponent={({item}) => <Text>{item.key}</Text>}
149159 * />
150160 */
151- class FlatList extends React . PureComponent {
152- static defaultProps = {
153- keyExtractor : VirtualizedList . defaultProps . keyExtractor ,
154- numColumns : 1 ,
155- shouldItemUpdate : VirtualizedList . defaultProps . shouldItemUpdate ,
156- } ;
157- props : Props ;
161+ class FlatList < ItemT > extends React.PureComponent< DefaultProps , Props < ItemT > , void> {
162+ static defaultProps : DefaultProps = defaultProps ;
163+ props : Props < ItemT > ;
158164 /**
159165 * Scrolls to the end of the content. May be janky without getItemLayout prop.
160166 */
@@ -191,7 +197,7 @@ class FlatList extends React.PureComponent {
191197 this . _checkProps ( this . props ) ;
192198 }
193199
194- componentWillReceiveProps ( nextProps : Props ) {
200+ componentWillReceiveProps(nextProps: Props< ItemT > ) {
195201 this . _checkProps ( nextProps ) ;
196202 }
197203
@@ -200,7 +206,7 @@ class FlatList extends React.PureComponent {
200206
201207 _captureRef = (ref) => { this . _listRef = ref ; } ;
202208
203- _checkProps ( props : Props ) {
209+ _checkProps(props: Props< ItemT > ) {
204210 const {
205211 getItem,
206212 getItemCount,
@@ -229,7 +235,7 @@ class FlatList extends React.PureComponent {
229235 }
230236 }
231237
232- _getItem = ( data : Array < Item > , index : number ) : Item | Array < Item > => {
238+ _getItem = ( data : Array < ItemT > , index: number): ItemT | Array< ItemT > => {
233239 const { numColumns} = this . props ;
234240 if ( numColumns > 1 ) {
235241 const ret = [ ] ;
@@ -243,13 +249,19 @@ class FlatList extends React.PureComponent {
243249 }
244250 } ;
245251
246- _getItemCount = ( data : Array < Item > ) : number => {
252+ _getItemCount = (data: Array< ItemT > ): number => {
247253 return Math . floor ( data . length / this . props . numColumns ) ;
248254 } ;
249255
250- _keyExtractor = ( items : Item | Array < Item > , index : number ) : string => {
256+ _keyExtractor = (items: ItemT | Array< ItemT > , index: number): string => {
251257 const { keyExtractor, numColumns} = this . props ;
252258 if ( numColumns > 1 ) {
259+ invariant (
260+ Array . isArray ( items ) ,
261+ 'FlatList: Encountered internal consistency error, expected each item to consist of an ' +
262+ 'array with 1-%s columns; instead, received a single item.' ,
263+ numColumns ,
264+ ) ;
253265 return items . map ( ( it , kk ) => keyExtractor ( it , index * numColumns + kk ) ) . join ( ':' ) ;
254266 } else {
255267 return keyExtractor ( items , index ) ;
0 commit comments