@@ -22,7 +22,10 @@ const infoLog = require('../Utilities/infoLog');
2222const invariant = require ( 'invariant' ) ;
2323import VirtualizedListInjection from './VirtualizedListInjection' ;
2424
25- import { computeWindowedRenderLimits } from './VirtualizeUtils' ;
25+ import {
26+ keyExtractor as defaultKeyExtractor ,
27+ computeWindowedRenderLimits ,
28+ } from './VirtualizeUtils' ;
2629
2730import * as React from 'react' ;
2831import type { ScrollResponderType } from '../Components/ScrollView/ScrollView' ;
@@ -132,7 +135,7 @@ type OptionalProps = {|
132135 * Reverses the direction of scroll. Uses scale transforms of -1.
133136 */
134137 inverted ?: ?boolean ,
135- keyExtractor : ( item : Item , index : number ) => string ,
138+ keyExtractor ?: ? ( item : Item , index : number ) => string ,
136139 /**
137140 * Each cell is rendered using this element. Can be a React Component Class,
138141 * or a render function. Defaults to using View.
@@ -303,10 +306,6 @@ type Props = {|
303306 ...OptionalProps ,
304307| } ;
305308
306- type DefaultProps = { |
307- keyExtractor : ( item : Item , index : number ) => string ,
308- | } ;
309-
310309let _usedIndexForKey = false ;
311310let _keylessItemComponentName : string = '' ;
312311
@@ -315,26 +314,37 @@ type State = {
315314 last : number ,
316315} ;
317316
317+ /**
318+ * Default Props Helper Functions
319+ * Use the following helper functions for default values
320+ */
321+
322+ // horizontalOrDefault(this.props.horizontal)
318323function horizontalOrDefault ( horizontal : ?boolean ) {
319324 return horizontal ?? false ;
320325}
321326
327+ // initialNumToRenderOrDefault(this.props.initialNumToRenderOrDefault)
322328function initialNumToRenderOrDefault ( initialNumToRender : ?number ) {
323329 return initialNumToRender ?? 10 ;
324330}
325331
332+ // maxToRenderPerBatchOrDefault(this.props.maxToRenderPerBatch)
326333function maxToRenderPerBatchOrDefault ( maxToRenderPerBatch : ?number ) {
327334 return maxToRenderPerBatch ?? 10 ;
328335}
329336
337+ // onEndReachedThresholdOrDefault(this.props.onEndReachedThreshold)
330338function onEndReachedThresholdOrDefault ( onEndReachedThreshold : ?number ) {
331339 return onEndReachedThreshold ?? 2 ;
332340}
333341
342+ // scrollEventThrottleOrDefault(this.props.scrollEventThrottle)
334343function scrollEventThrottleOrDefault ( scrollEventThrottle : ?number ) {
335344 return scrollEventThrottle ?? 50 ;
336345}
337346
347+ // windowSizeOrDefault(this.props.windowSize)
338348function windowSizeOrDefault ( windowSize : ?number ) {
339349 return windowSize ?? 21 ;
340350}
@@ -365,6 +375,7 @@ function windowSizeOrDefault(windowSize: ?number) {
365375 * and we are working on improving it behind the scenes.
366376 * - By default, the list looks for a `key` or `id` prop on each item and uses that for the React key.
367377 * Alternatively, you can provide a custom `keyExtractor` prop.
378+ * - As an effort to remove defaultProps, use helper functions when referencing certain props
368379 *
369380 */
370381class VirtualizedList extends React . PureComponent < Props , State > {
@@ -580,22 +591,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
580591 }
581592 }
582593
583- static defaultProps : DefaultProps = {
584- keyExtractor : ( item : Item , index : number ) => {
585- if ( item . key != null ) {
586- return item . key ;
587- }
588- if ( item . id != null ) {
589- return item . id ;
590- }
591- _usedIndexForKey = true ;
592- if ( item . type && item . type . displayName ) {
593- _keylessItemComponentName = item . type . displayName ;
594- }
595- return String ( index ) ;
596- } ,
597- } ;
598-
599594 _getCellKey ( ) : string {
600595 return this . context ?. cellKey || 'rootList' ;
601596 }
@@ -804,15 +799,14 @@ class VirtualizedList extends React.PureComponent<Props, State> {
804799 getItem,
805800 getItemCount,
806801 horizontal,
807- keyExtractor,
808802 } = this . props ;
809803 const stickyOffset = this . props . ListHeaderComponent ? 1 : 0 ;
810804 const end = getItemCount ( data ) - 1 ;
811805 let prevCellKey ;
812806 last = Math . min ( end , last ) ;
813807 for ( let ii = first ; ii <= last ; ii ++ ) {
814808 const item = getItem ( data , ii ) ;
815- const key = keyExtractor ( item , ii ) ;
809+ const key = this . _keyExtractor ( item , ii ) ;
816810 this . _indicesToKeys . set ( ii , key ) ;
817811 if ( stickyIndicesFromProps . has ( ii + stickyOffset ) ) {
818812 stickyHeaderIndices . push ( cells . length ) ;
@@ -864,6 +858,21 @@ class VirtualizedList extends React.PureComponent<Props, State> {
864858 _getSpacerKey = ( isVertical : boolean ) : string =>
865859 isVertical ? 'height' : 'width' ;
866860
861+ _keyExtractor ( item : Item , index : number ) {
862+ if ( this . props . keyExtractor != null ) {
863+ return this . props . keyExtractor ( item , index ) ;
864+ }
865+
866+ const key = defaultKeyExtractor ( item , index ) ;
867+ if ( key === String ( index ) ) {
868+ _usedIndexForKey = true ;
869+ if ( item . type && item . type . displayName ) {
870+ _keylessItemComponentName = item . type . displayName ;
871+ }
872+ }
873+ return key ;
874+ }
875+
867876 render ( ) : React . Node {
868877 if ( __DEV__ ) {
869878 const flatStyles = flattenStyle ( this . props . contentContainerStyle ) ;
@@ -1816,9 +1825,9 @@ class VirtualizedList extends React.PureComponent<Props, State> {
18161825 } ;
18171826
18181827 _createViewToken = ( index : number , isViewable : boolean ) => {
1819- const { data, getItem, keyExtractor } = this . props ;
1828+ const { data, getItem} = this . props ;
18201829 const item = getItem ( data , index ) ;
1821- return { index, item, key : keyExtractor ( item , index ) , isViewable} ;
1830+ return { index, item, key : this . _keyExtractor ( item , index ) , isViewable} ;
18221831 } ;
18231832
18241833 _getFrameMetricsApprox = (
@@ -1854,19 +1863,13 @@ class VirtualizedList extends React.PureComponent<Props, State> {
18541863 inLayout ? : boolean ,
18551864 ...
18561865 } => {
1857- const {
1858- data,
1859- getItem,
1860- getItemCount,
1861- getItemLayout,
1862- keyExtractor,
1863- } = this . props ;
1866+ const { data, getItem, getItemCount, getItemLayout} = this . props ;
18641867 invariant (
18651868 getItemCount ( data ) > index ,
18661869 'Tried to get frame for out of range index ' + index ,
18671870 ) ;
18681871 const item = getItem ( data , index ) ;
1869- let frame = item && this . _frames [ keyExtractor ( item , index ) ] ;
1872+ let frame = item && this . _frames [ this . _keyExtractor ( item , index ) ] ;
18701873 if ( ! frame || frame . index !== index ) {
18711874 if ( getItemLayout ) {
18721875 frame = getItemLayout ( data , index ) ;
0 commit comments