Skip to content

Commit 6d1eeb0

Browse files
committed
chore(CheckBox): getDerivedStateFromProps replace UNSAFE_componentWillReceiveProps
1 parent fc0a9d9 commit 6d1eeb0

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/core/src/CheckBox/index.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ export interface CheckBoxProps extends TouchableOpacityProps {
2626

2727
export interface CheckBoxState {
2828
checked: boolean;
29+
controlChecked: 'props' | 'state';
2930
}
3031

3132
export default class CheckBox extends React.Component<CheckBoxProps, CheckBoxState> {
3233
constructor(props: CheckBoxProps) {
3334
super(props);
3435
this.state = {
3536
checked: !!props.checked,
37+
controlChecked: 'props',
3638
};
3739
}
3840
static defaultProps = {
@@ -41,14 +43,23 @@ export default class CheckBox extends React.Component<CheckBoxProps, CheckBoxSta
4143
color: '#008EF0',
4244
size: 16,
4345
};
44-
UNSAFE_componentWillReceiveProps(nextProps: CheckBoxProps) {
45-
if (nextProps.checked !== this.props.checked) {
46-
this.setState({ checked: !!nextProps.checked });
46+
static getDerivedStateFromProps(props: CheckBoxProps, state: CheckBoxState) {
47+
if (props.checked === state.checked && state.controlChecked === 'props') {
48+
return null;
4749
}
50+
if (state.controlChecked === 'props') {
51+
return {
52+
checked: props.checked,
53+
};
54+
}
55+
return {
56+
controlChecked: 'props',
57+
};
4858
}
59+
4960
onPress = () => {
5061
const { onChange } = this.props;
51-
this.setState({ checked: !this.state.checked }, () => {
62+
this.setState({ checked: !this.state.checked, controlChecked: 'state' }, () => {
5263
onChange && onChange(this.state.checked);
5364
});
5465
};

0 commit comments

Comments
 (0)