Skip to content

Commit f293d41

Browse files
jimmy623facebook-github-bot
authored andcommitted
Back out "Update typed export and fix test", Back out "[VirtualizedSectionList] Remove defaultProps", Back out "[VirtualizedList] Remove keyExtractor defaultProps"
Summary: Changelog: Partial revert the stack Reviewed By: makovkastar Differential Revision: D27156625 fbshipit-source-id: b158c9102047bb64ce708b200a8e5786f5a0c176
1 parent 729c6d2 commit f293d41

File tree

8 files changed

+104
-255
lines changed

8 files changed

+104
-255
lines changed

Libraries/Lists/FlatList.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import type {
2525
ViewabilityConfigCallbackPair,
2626
} from './ViewabilityHelper';
2727
import type {RenderItemType, RenderItemProps} from './VirtualizedList';
28-
import {keyExtractor as defaultKeyExtractor} from './VirtualizeUtils';
2928

3029
type RequiredProps<ItemT> = {|
3130
/**
@@ -121,7 +120,7 @@ type OptionalProps<ItemT> = {|
121120
* and as the react key to track item re-ordering. The default extractor checks `item.key`, then
122121
* falls back to using the index, like React does.
123122
*/
124-
keyExtractor?: ?(item: ItemT, index: number) => string,
123+
keyExtractor: (item: ItemT, index: number) => string,
125124
/**
126125
* Multiple columns can only be rendered with `horizontal={false}` and will zig-zag like a
127126
* `flexWrap` layout. Items should all be the same height - masonry layouts are not supported.
@@ -157,6 +156,7 @@ export type Props<ItemT> = {
157156
};
158157

159158
const defaultProps = {
159+
...VirtualizedList.defaultProps,
160160
numColumns: 1,
161161
/**
162162
* Enabling this prop on Android greatly improves scrolling performance with no known issues.
@@ -503,33 +503,28 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
503503
};
504504

505505
_keyExtractor = (items: ItemT | Array<ItemT>, index: number) => {
506-
const {numColumns} = this.props;
507-
const keyExtractor = this.props.keyExtractor ?? defaultKeyExtractor;
508-
506+
const {keyExtractor, numColumns} = this.props;
509507
if (numColumns > 1) {
510-
if (Array.isArray(items)) {
511-
return items
512-
.map((item, kk) =>
513-
keyExtractor(((item: $FlowFixMe): ItemT), index * numColumns + kk),
514-
)
515-
.join(':');
516-
} else {
517-
invariant(
518-
Array.isArray(items),
519-
'FlatList: Encountered internal consistency error, expected each item to consist of an ' +
520-
'array with 1-%s columns; instead, received a single item.',
521-
numColumns,
522-
);
523-
}
508+
invariant(
509+
Array.isArray(items),
510+
'FlatList: Encountered internal consistency error, expected each item to consist of an ' +
511+
'array with 1-%s columns; instead, received a single item.',
512+
numColumns,
513+
);
514+
return (
515+
items
516+
// $FlowFixMe[incompatible-call]
517+
.map((it, kk) => keyExtractor(it, index * numColumns + kk))
518+
.join(':')
519+
);
524520
} else {
525521
// $FlowFixMe Can't call keyExtractor with an array
526522
return keyExtractor(items, index);
527523
}
528524
};
529525

530526
_pushMultiColumnViewable(arr: Array<ViewToken>, v: ViewToken): void {
531-
const {numColumns} = this.props;
532-
const keyExtractor = this.props.keyExtractor ?? defaultKeyExtractor;
527+
const {numColumns, keyExtractor} = this.props;
533528
v.item.forEach((item, ii) => {
534529
invariant(v.index != null, 'Missing index!');
535530
const index = v.index * numColumns + ii;

Libraries/Lists/SectionList.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type OptionalProps<SectionT: SectionBase<any>> = {|
7878
* falls back to using the index, like react does. Note that this sets keys for each item, but
7979
* each overall section still needs its own key.
8080
*/
81-
keyExtractor?: ?(item: Item, index: number) => string,
81+
keyExtractor: (item: Item, index: number) => string,
8282
/**
8383
* Called once when the scroll position gets within `onEndReachedThreshold` of the rendered
8484
* content.
@@ -105,10 +105,6 @@ export type Props<SectionT> = {|
105105
VirtualizedSectionListProps<SectionT>,
106106
'renderItem',
107107
>,
108-
keyExtractor: $PropertyType<
109-
VirtualizedSectionListProps<SectionT>,
110-
'keyExtractor',
111-
>,
112108
...
113109
},
114110
>,
@@ -117,6 +113,7 @@ export type Props<SectionT> = {|
117113
|};
118114

119115
const defaultProps = {
116+
...VirtualizedSectionList.defaultProps,
120117
stickySectionHeadersEnabled: Platform.OS === 'ios',
121118
};
122119

Libraries/Lists/VirtualizeUtils.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,3 @@ export function computeWindowedRenderLimits(
233233
}
234234
return {first, last};
235235
}
236-
237-
export function keyExtractor(item: any, index: number): string {
238-
if (typeof item === 'object' && item?.key != null) {
239-
return item.key;
240-
}
241-
if (typeof item === 'object' && item?.id != null) {
242-
return item.id;
243-
}
244-
return String(index);
245-
}

Libraries/Lists/VirtualizedList.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ const infoLog = require('../Utilities/infoLog');
2222
const invariant = require('invariant');
2323
import VirtualizedListInjection from './VirtualizedListInjection';
2424

25-
import {
26-
keyExtractor as defaultKeyExtractor,
27-
computeWindowedRenderLimits,
28-
} from './VirtualizeUtils';
25+
import {computeWindowedRenderLimits} from './VirtualizeUtils';
2926

3027
import * as React from 'react';
3128
import type {ScrollResponderType} from '../Components/ScrollView/ScrollView';
@@ -135,7 +132,7 @@ type OptionalProps = {|
135132
* Reverses the direction of scroll. Uses scale transforms of -1.
136133
*/
137134
inverted?: ?boolean,
138-
keyExtractor?: ?(item: Item, index: number) => string,
135+
keyExtractor: (item: Item, index: number) => string,
139136
/**
140137
* Each cell is rendered using this element. Can be a React Component Class,
141138
* or a render function. Defaults to using View.
@@ -306,6 +303,10 @@ type Props = {|
306303
...OptionalProps,
307304
|};
308305

306+
type DefaultProps = {|
307+
keyExtractor: (item: Item, index: number) => string,
308+
|};
309+
309310
let _usedIndexForKey = false;
310311
let _keylessItemComponentName: string = '';
311312

@@ -314,37 +315,26 @@ type State = {
314315
last: number,
315316
};
316317

317-
/**
318-
* Default Props Helper Functions
319-
* Use the following helper functions for default values
320-
*/
321-
322-
// horizontalOrDefault(this.props.horizontal)
323318
function horizontalOrDefault(horizontal: ?boolean) {
324319
return horizontal ?? false;
325320
}
326321

327-
// initialNumToRenderOrDefault(this.props.initialNumToRenderOrDefault)
328322
function initialNumToRenderOrDefault(initialNumToRender: ?number) {
329323
return initialNumToRender ?? 10;
330324
}
331325

332-
// maxToRenderPerBatchOrDefault(this.props.maxToRenderPerBatch)
333326
function maxToRenderPerBatchOrDefault(maxToRenderPerBatch: ?number) {
334327
return maxToRenderPerBatch ?? 10;
335328
}
336329

337-
// onEndReachedThresholdOrDefault(this.props.onEndReachedThreshold)
338330
function onEndReachedThresholdOrDefault(onEndReachedThreshold: ?number) {
339331
return onEndReachedThreshold ?? 2;
340332
}
341333

342-
// scrollEventThrottleOrDefault(this.props.scrollEventThrottle)
343334
function scrollEventThrottleOrDefault(scrollEventThrottle: ?number) {
344335
return scrollEventThrottle ?? 50;
345336
}
346337

347-
// windowSizeOrDefault(this.props.windowSize)
348338
function windowSizeOrDefault(windowSize: ?number) {
349339
return windowSize ?? 21;
350340
}
@@ -375,7 +365,6 @@ function windowSizeOrDefault(windowSize: ?number) {
375365
* and we are working on improving it behind the scenes.
376366
* - By default, the list looks for a `key` or `id` prop on each item and uses that for the React key.
377367
* Alternatively, you can provide a custom `keyExtractor` prop.
378-
* - As an effort to remove defaultProps, use helper functions when referencing certain props
379368
*
380369
*/
381370
class VirtualizedList extends React.PureComponent<Props, State> {
@@ -591,6 +580,22 @@ class VirtualizedList extends React.PureComponent<Props, State> {
591580
}
592581
}
593582

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+
594599
_getCellKey(): string {
595600
return this.context?.cellKey || 'rootList';
596601
}
@@ -799,14 +804,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
799804
getItem,
800805
getItemCount,
801806
horizontal,
807+
keyExtractor,
802808
} = this.props;
803809
const stickyOffset = this.props.ListHeaderComponent ? 1 : 0;
804810
const end = getItemCount(data) - 1;
805811
let prevCellKey;
806812
last = Math.min(end, last);
807813
for (let ii = first; ii <= last; ii++) {
808814
const item = getItem(data, ii);
809-
const key = this._keyExtractor(item, ii);
815+
const key = keyExtractor(item, ii);
810816
this._indicesToKeys.set(ii, key);
811817
if (stickyIndicesFromProps.has(ii + stickyOffset)) {
812818
stickyHeaderIndices.push(cells.length);
@@ -858,21 +864,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
858864
_getSpacerKey = (isVertical: boolean): string =>
859865
isVertical ? 'height' : 'width';
860866

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-
876867
render(): React.Node {
877868
if (__DEV__) {
878869
const flatStyles = flattenStyle(this.props.contentContainerStyle);
@@ -1825,9 +1816,9 @@ class VirtualizedList extends React.PureComponent<Props, State> {
18251816
};
18261817

18271818
_createViewToken = (index: number, isViewable: boolean) => {
1828-
const {data, getItem} = this.props;
1819+
const {data, getItem, keyExtractor} = this.props;
18291820
const item = getItem(data, index);
1830-
return {index, item, key: this._keyExtractor(item, index), isViewable};
1821+
return {index, item, key: keyExtractor(item, index), isViewable};
18311822
};
18321823

18331824
_getFrameMetricsApprox = (
@@ -1863,13 +1854,19 @@ class VirtualizedList extends React.PureComponent<Props, State> {
18631854
inLayout?: boolean,
18641855
...
18651856
} => {
1866-
const {data, getItem, getItemCount, getItemLayout} = this.props;
1857+
const {
1858+
data,
1859+
getItem,
1860+
getItemCount,
1861+
getItemLayout,
1862+
keyExtractor,
1863+
} = this.props;
18671864
invariant(
18681865
getItemCount(data) > index,
18691866
'Tried to get frame for out of range index ' + index,
18701867
);
18711868
const item = getItem(data, index);
1872-
let frame = item && this._frames[this._keyExtractor(item, index)];
1869+
let frame = item && this._frames[keyExtractor(item, index)];
18731870
if (!frame || frame.index !== index) {
18741871
if (getItemLayout) {
18751872
frame = getItemLayout(data, index);

Libraries/Lists/VirtualizedSectionList.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const VirtualizedList = require('./VirtualizedList');
1717
const invariant = require('invariant');
1818

1919
import type {ViewToken} from './ViewabilityHelper';
20-
import {keyExtractor as defaultKeyExtractor} from './VirtualizeUtils';
2120

2221
type Item = any;
2322

@@ -100,18 +99,14 @@ type OptionalProps<SectionT: SectionBase<any>> = {|
10099
onEndReached?: ?({distanceFromEnd: number, ...}) => void,
101100
|};
102101

103-
type VirtualizedListProps = React.ElementConfig<typeof VirtualizedList>;
102+
type VirtualizedListProps = React.ElementProps<typeof VirtualizedList>;
104103

105104
export type Props<SectionT> = {|
106105
...RequiredProps<SectionT>,
107106
...OptionalProps<SectionT>,
108107
...$Diff<
109108
VirtualizedListProps,
110-
{
111-
renderItem: $PropertyType<VirtualizedListProps, 'renderItem'>,
112-
data: $PropertyType<VirtualizedListProps, 'data'>,
113-
...
114-
},
109+
{renderItem: $PropertyType<VirtualizedListProps, 'renderItem'>, ...},
115110
>,
116111
|};
117112
export type ScrollToLocationParamsType = {|
@@ -122,6 +117,11 @@ export type ScrollToLocationParamsType = {|
122117
viewPosition?: number,
123118
|};
124119

120+
type DefaultProps = {|
121+
...typeof VirtualizedList.defaultProps,
122+
data: $ReadOnlyArray<Item>,
123+
|};
124+
125125
type State = {childProps: VirtualizedListProps, ...};
126126

127127
/**
@@ -132,6 +132,11 @@ type State = {childProps: VirtualizedListProps, ...};
132132
class VirtualizedSectionList<
133133
SectionT: SectionBase<any>,
134134
> extends React.PureComponent<Props<SectionT>, State> {
135+
static defaultProps: DefaultProps = {
136+
...VirtualizedList.defaultProps,
137+
data: [],
138+
};
139+
135140
scrollToLocation(params: ScrollToLocationParamsType) {
136141
let index = params.itemIndex;
137142
for (let i = 0; i < params.sectionIndex; i++) {
@@ -212,11 +217,11 @@ class VirtualizedSectionList<
212217
);
213218
}
214219

215-
_getItem(
220+
_getItem = (
216221
props: Props<SectionT>,
217222
sections: ?$ReadOnlyArray<Item>,
218223
index: number,
219-
): ?Item {
224+
): ?Item => {
220225
if (!sections) {
221226
return null;
222227
}
@@ -238,7 +243,7 @@ class VirtualizedSectionList<
238243
}
239244
}
240245
return null;
241-
}
246+
};
242247

243248
_keyExtractor = (item: Item, index: number) => {
244249
const info = this._subExtractor(index);
@@ -287,8 +292,7 @@ class VirtualizedSectionList<
287292
trailingSection: sections[i + 1],
288293
};
289294
} else {
290-
const extractor =
291-
section.keyExtractor || keyExtractor || defaultKeyExtractor;
295+
const extractor = section.keyExtractor || keyExtractor;
292296
return {
293297
section,
294298
key:
@@ -577,11 +581,4 @@ class ItemWithSeparator extends React.Component<
577581
}
578582
}
579583

580-
module.exports = (VirtualizedSectionList: React.AbstractComponent<
581-
React.ElementConfig<typeof VirtualizedSectionList>,
582-
$ReadOnly<{
583-
getListRef: () => ?React.ElementRef<typeof VirtualizedList>,
584-
scrollToLocation: (params: ScrollToLocationParamsType) => void,
585-
...
586-
}>,
587-
>);
584+
module.exports = VirtualizedSectionList;

0 commit comments

Comments
 (0)