Skip to content

Commit f17c8d1

Browse files
committed
chore(Radio): getDerivedStateFromProps replace UNSAFE_componentWillReceiveProps
1 parent 9103247 commit f17c8d1

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

packages/core/src/List/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default class List extends React.PureComponent<ListProps, ListState> {
7777
data: result,
7878
};
7979
}
80+
return null;
8081
}
8182
renderItemChild(props: ListRenderItemInfoCustom<{}>): React.ReactElement {
8283
return props.item as React.ReactElement;

packages/core/src/Radio/index.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface RadioProps extends ViewProps {
5555
export interface RadioState {
5656
sizeValue: Animated.Value;
5757
checked?: boolean;
58+
control: 'state' | 'props';
5859
}
5960

6061
export default class Radio extends Component<RadioProps, RadioState> {
@@ -69,39 +70,41 @@ export default class Radio extends Component<RadioProps, RadioState> {
6970
this.state = {
7071
checked: props.checked,
7172
sizeValue: new Animated.Value(0),
73+
control: 'state',
7274
};
7375
}
7476
componentDidMount() {
75-
// this.setState({
76-
// checked: this.props.checked,
77-
// });
7877
this.animatedStart(this.props.checked);
7978
}
80-
UNSAFE_componentWillReceiveProps(nextProps: RadioProps) {
81-
if (nextProps.checked !== this.props.checked) {
82-
this.setState({ checked: nextProps.checked }, () => {
83-
this.animatedStart(nextProps.checked);
84-
});
79+
static getDerivedStateFromProps(props: RadioProps, state: RadioState) {
80+
if (state.control === 'state' && props.checked === state.checked) {
81+
return {
82+
control: 'props',
83+
};
8584
}
86-
}
87-
animatedStart(checked?: boolean) {
88-
if (checked) {
89-
Animated.spring(this.state.sizeValue, {
90-
toValue: this.props.thumbSize!,
91-
overshootClamping: true,
92-
useNativeDriver: false,
93-
}).start();
94-
} else {
95-
Animated.spring(this.state.sizeValue, {
96-
toValue: 0,
85+
if (props.checked !== state.checked) {
86+
Animated.spring(state.sizeValue, {
87+
toValue: !!props.checked ? props.thumbSize! : 0,
9788
overshootClamping: true,
9889
useNativeDriver: false,
9990
}).start();
91+
return {
92+
checked: props.checked,
93+
control: 'props',
94+
};
10095
}
96+
return null;
97+
}
98+
animatedStart(checked?: boolean) {
99+
Animated.spring(this.state.sizeValue, {
100+
toValue: !!checked ? this.props.thumbSize! : 0,
101+
overshootClamping: true,
102+
useNativeDriver: false,
103+
}).start();
101104
}
102105
handlePress = (event: GestureResponderEvent) => {
103106
const { onPress } = this.props;
104-
this.setState({ checked: true }, () => {
107+
this.setState({ checked: true, control: 'state' }, () => {
105108
this.animatedStart(true);
106109
onPress && onPress(event);
107110
});

0 commit comments

Comments
 (0)