Skip to content

Commit 9103247

Browse files
committed
chore(List): getDerivedStateFromProps replace UNSAFE_componentWillReceiveProps
1 parent ee16d09 commit 9103247

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

packages/core/src/List/Item.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import {
33
View,
44
ViewProps,
@@ -23,7 +23,7 @@ export interface ListItemProps extends ViewProps, TouchableWithoutFeedbackProps
2323
size?: 'small' | 'default' | 'large';
2424
}
2525

26-
export default class ListItem extends Component<ListItemProps> {
26+
export default class ListItem extends React.PureComponent<ListItemProps> {
2727
static defaultProps: ListItemProps = {
2828
underlayColor: '#DADADA',
2929
// paddingLeft: 16,

packages/core/src/List/index.tsx

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { FlatList, FlatListProps, Text, StyleProp, ViewStyle, View } from 'react-native';
33
import Item, { ListItemProps } from './Item';
44

@@ -43,7 +43,7 @@ export interface ListState {
4343
data: ListProps['data'];
4444
}
4545

46-
export default class List extends Component<ListProps, ListState> {
46+
export default class List extends React.PureComponent<ListProps, ListState> {
4747
static Item = Item;
4848
static defaultProps: ListProps = {
4949
data: [],
@@ -57,27 +57,25 @@ export default class List extends Component<ListProps, ListState> {
5757
data: [],
5858
};
5959
}
60-
getData(nextProps?: ListProps) {
61-
const { size, extra, paddingLeft, children } = nextProps || this.props;
62-
const dataSource = React.Children.toArray(children)
63-
.map((child: React.ReactNode) => {
64-
if (!React.isValidElement(child)) {
65-
return null;
66-
}
67-
const props = { size, ...child.props };
68-
return React.cloneElement(<Item paddingLeft={paddingLeft} extra={extra} {...props} />);
69-
})
70-
.filter(Boolean);
71-
this.setState({ data: dataSource as ListProps['data'] });
72-
}
73-
componentDidMount() {
74-
if (!this.props.renderItem) {
75-
this.getData();
76-
}
77-
}
78-
UNSAFE_componentWillReceiveProps(nextProps: ListProps) {
79-
if (nextProps !== this.props) {
80-
this.getData(nextProps);
60+
static getDerivedStateFromProps(props: ListProps) {
61+
const getData = () => {
62+
const { size, extra, paddingLeft, children } = props;
63+
const dataSource = React.Children.toArray(children)
64+
.map((child: React.ReactNode) => {
65+
if (!React.isValidElement(child)) {
66+
return null;
67+
}
68+
const props = { size, ...child.props };
69+
return React.cloneElement(<Item paddingLeft={paddingLeft} extra={extra} {...props} />);
70+
})
71+
.filter(Boolean);
72+
return dataSource;
73+
};
74+
if (!props.renderItem) {
75+
const result = getData();
76+
return {
77+
data: result,
78+
};
8179
}
8280
}
8381
renderItemChild(props: ListRenderItemInfoCustom<{}>): React.ReactElement {

packages/core/src/SelectCascader/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { View, Text, StyleSheet, TouchableHighlight, StyleProp, TextStyle, ViewStyle } from 'react-native';
2-
import React, { Component, ReactNode } from 'react';
2+
import React, { Component } from 'react';
33
import { Picker } from '@react-native-picker/picker';
44
import arrayTreeFilter from '../utils/arrayTreeFilter';
55
import Modal from '../Modal';

0 commit comments

Comments
 (0)