Skip to content

Commit a90d0e3

Browse files
sahrensfacebook-github-bot
authored andcommitted
fixup FlatList
Summary: StrictMode compliance is important for Async Rendering and other Future Tech(tm). Also clean up some lint. == Test Plan == * No more StrictMode warnings for `FlatList` (`VirtualizedList` is another story....). * props warnings still show up when appropriate. Reviewed By: sophiebits Differential Revision: D8026333 fbshipit-source-id: e6fa2f02d546b883df6f3cff8776c26c6ef91bc9
1 parent 26a1eba commit a90d0e3

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

Libraries/Lists/FlatList.js

+35-31
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const React = require('React');
1414
const View = require('View');
1515
const VirtualizedList = require('VirtualizedList');
1616
const ListView = require('ListView');
17+
const StyleSheet = require('StyleSheet');
1718

1819
const invariant = require('fbjs/lib/invariant');
1920

@@ -421,41 +422,15 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
421422
}
422423
}
423424

424-
setNativeProps(props: Object) {
425+
setNativeProps(props: {[string]: mixed}) {
425426
if (this._listRef) {
426427
this._listRef.setNativeProps(props);
427428
}
428429
}
429430

430-
UNSAFE_componentWillMount() {
431-
this._checkProps(this.props);
432-
}
433-
434-
UNSAFE_componentWillReceiveProps(nextProps: Props<ItemT>) {
435-
invariant(
436-
nextProps.numColumns === this.props.numColumns,
437-
'Changing numColumns on the fly is not supported. Change the key prop on FlatList when ' +
438-
'changing the number of columns to force a fresh render of the component.',
439-
);
440-
invariant(
441-
nextProps.onViewableItemsChanged === this.props.onViewableItemsChanged,
442-
'Changing onViewableItemsChanged on the fly is not supported',
443-
);
444-
invariant(
445-
nextProps.viewabilityConfig === this.props.viewabilityConfig,
446-
'Changing viewabilityConfig on the fly is not supported',
447-
);
448-
invariant(
449-
nextProps.viewabilityConfigCallbackPairs ===
450-
this.props.viewabilityConfigCallbackPairs,
451-
'Changing viewabilityConfigCallbackPairs on the fly is not supported',
452-
);
453-
454-
this._checkProps(nextProps);
455-
}
456-
457-
constructor(props: Props<*>) {
431+
constructor(props: Props<ItemT>) {
458432
super(props);
433+
this._checkProps(this.props);
459434
if (this.props.viewabilityConfigCallbackPairs) {
460435
this._virtualizedListPairs = this.props.viewabilityConfigCallbackPairs.map(
461436
pair => ({
@@ -478,6 +453,29 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
478453
}
479454
}
480455

456+
componentDidUpdate(prevProps: Props<ItemT>) {
457+
invariant(
458+
prevProps.numColumns === this.props.numColumns,
459+
'Changing numColumns on the fly is not supported. Change the key prop on FlatList when ' +
460+
'changing the number of columns to force a fresh render of the component.',
461+
);
462+
invariant(
463+
prevProps.onViewableItemsChanged === this.props.onViewableItemsChanged,
464+
'Changing onViewableItemsChanged on the fly is not supported',
465+
);
466+
invariant(
467+
prevProps.viewabilityConfig === this.props.viewabilityConfig,
468+
'Changing viewabilityConfig on the fly is not supported',
469+
);
470+
invariant(
471+
prevProps.viewabilityConfigCallbackPairs ===
472+
this.props.viewabilityConfigCallbackPairs,
473+
'Changing viewabilityConfigCallbackPairs on the fly is not supported',
474+
);
475+
476+
this._checkProps(this.props);
477+
}
478+
481479
_hasWarnedLegacy = false;
482480
_listRef: null | VirtualizedList | ListView | MetroListView;
483481
_virtualizedListPairs: Array<ViewabilityConfigCallbackPair> = [];
@@ -537,7 +535,9 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
537535
const ret = [];
538536
for (let kk = 0; kk < numColumns; kk++) {
539537
const item = data[index * numColumns + kk];
540-
item && ret.push(item);
538+
if (item != null) {
539+
ret.push(item);
540+
}
541541
}
542542
return ret;
543543
} else {
@@ -614,7 +614,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
614614
'Expected array of items with numColumns > 1',
615615
);
616616
return (
617-
<View style={[{flexDirection: 'row'}, columnWrapperStyle]}>
617+
<View style={StyleSheet.compose(styles.row, columnWrapperStyle)}>
618618
{item.map((it, kk) => {
619619
const element = renderItem({
620620
item: it,
@@ -661,4 +661,8 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
661661
}
662662
}
663663

664+
const styles = StyleSheet.create({
665+
row: {flexDirection: 'row'},
666+
});
667+
664668
module.exports = FlatList;

Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap

+9-18
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,9 @@ exports[`FlatList renders all the bells and whistles 1`] = `
7272
>
7373
<View
7474
style={
75-
Array [
76-
Object {
77-
"flexDirection": "row",
78-
},
79-
undefined,
80-
]
75+
Object {
76+
"flexDirection": "row",
77+
}
8178
}
8279
>
8380
<item
@@ -95,12 +92,9 @@ exports[`FlatList renders all the bells and whistles 1`] = `
9592
>
9693
<View
9794
style={
98-
Array [
99-
Object {
100-
"flexDirection": "row",
101-
},
102-
undefined,
103-
]
95+
Object {
96+
"flexDirection": "row",
97+
}
10498
}
10599
>
106100
<item
@@ -118,12 +112,9 @@ exports[`FlatList renders all the bells and whistles 1`] = `
118112
>
119113
<View
120114
style={
121-
Array [
122-
Object {
123-
"flexDirection": "row",
124-
},
125-
undefined,
126-
]
115+
Object {
116+
"flexDirection": "row",
117+
}
127118
}
128119
>
129120
<item

0 commit comments

Comments
 (0)