Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove defaultProps from FlatList #31798

Closed
wants to merge 13 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions Libraries/Lists/FlatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ type OptionalProps<ItemT> = {|
* Multiple columns can only be rendered with `horizontal={false}` and will zig-zag like a
* `flexWrap` layout. Items should all be the same height - masonry layouts are not supported.
daschaa marked this conversation as resolved.
Show resolved Hide resolved
*/
numColumns: number,
numColumns?: number,
/**
* Note: may have bugs (missing content) in some circumstances - use at your own risk.
*
* This may improve scroll performance for large lists.
daschaa marked this conversation as resolved.
Show resolved Hide resolved
*/
removeClippedSubviews?: boolean,
/**
* See `ScrollView` for flow type and further documentation.
*/
Expand Down Expand Up @@ -156,17 +162,6 @@ export type Props<ItemT> = {
...
};

const defaultProps = {
numColumns: 1,
/**
* Enabling this prop on Android greatly improves scrolling performance with no known issues.
* The alternative is that scrolling on Android is unusably bad. Enabling it on iOS has a few
* known issues.
*/
removeClippedSubviews: Platform.OS === 'android',
};
export type DefaultProps = typeof defaultProps;

/**
* A performant interface for rendering simple, flat lists, supporting the most handy features:
*
Expand Down Expand Up @@ -276,7 +271,6 @@ export type DefaultProps = typeof defaultProps;
* Also inherits [ScrollView Props](docs/scrollview.html#props), unless it is nested in another FlatList of same orientation.
*/
class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
static defaultProps: DefaultProps = defaultProps;
props: Props<ItemT>;
/**
* Scrolls to the end of the content. May be janky without `getItemLayout` prop.
Expand Down Expand Up @@ -453,7 +447,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
// $FlowFixMe[prop-missing] this prop doesn't exist, is only used for an invariant
getItemCount,
horizontal,
numColumns,
numColumns = 1,
columnWrapperStyle,
onViewableItemsChanged,
viewabilityConfigCallbackPairs,
Expand All @@ -478,7 +472,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
}

_getItem = (data: Array<ItemT>, index: number) => {
const {numColumns} = this.props;
const {numColumns = 1} = this.props;
if (numColumns > 1) {
const ret = [];
for (let kk = 0; kk < numColumns; kk++) {
Expand All @@ -495,15 +489,15 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {

_getItemCount = (data: ?Array<ItemT>): number => {
if (data) {
const {numColumns} = this.props;
const {numColumns = 1} = this.props;
return numColumns > 1 ? Math.ceil(data.length / numColumns) : data.length;
} else {
return 0;
}
};

_keyExtractor = (items: ItemT | Array<ItemT>, index: number) => {
const {numColumns} = this.props;
const {numColumns = 1} = this.props;
const keyExtractor = this.props.keyExtractor ?? defaultKeyExtractor;

if (numColumns > 1) {
Expand All @@ -528,7 +522,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
};

_pushMultiColumnViewable(arr: Array<ViewToken>, v: ViewToken): void {
const {numColumns} = this.props;
const {numColumns = 1} = this.props;
const keyExtractor = this.props.keyExtractor ?? defaultKeyExtractor;
v.item.forEach((item, ii) => {
invariant(v.index != null, 'Missing index!');
Expand All @@ -549,7 +543,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
changed: Array<ViewToken>,
...
}) => {
const {numColumns} = this.props;
const {numColumns = 1} = this.props;
if (onViewableItemsChanged) {
if (numColumns > 1) {
const changed = [];
Expand All @@ -570,7 +564,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
const {
ListItemComponent,
renderItem,
numColumns,
numColumns = 1,
daschaa marked this conversation as resolved.
Show resolved Hide resolved
columnWrapperStyle,
} = this.props;

Expand Down Expand Up @@ -625,7 +619,7 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
};

render(): React.Node {
const {numColumns, columnWrapperStyle, ...restProps} = this.props;
const {numColumns = 1, columnWrapperStyle, ...restProps} = this.props;

return (
<VirtualizedList
Expand All @@ -635,6 +629,9 @@ class FlatList<ItemT> extends React.PureComponent<Props<ItemT>, void> {
keyExtractor={this._keyExtractor}
ref={this._captureRef}
viewabilityConfigCallbackPairs={this._virtualizedListPairs}
removeClippedSubviews={
this.props.removeClippedSubviews || Platform.OS === 'android'
}
{...this._renderer()}
/>
);
Expand Down