Skip to content

Commit fa5dff0

Browse files
committed
chore(Drawer): getDerivedStateFromProps replace UNSAFE_componentWillReceiveProps
1 parent 4e2def2 commit fa5dff0

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

packages/core/src/Drawer/index.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export interface DrawerState {
2828
drawerValue: Animated.ValueXY;
2929
overlayValue: Animated.Value;
3030
zIndexValue: number;
31+
handleDrawer: (isOpen: boolean) => void;
32+
control: 'props' | 'state';
33+
isOpen: boolean;
3134
}
3235

3336
export default class Drawer extends Component<DrawerProps, DrawerState> {
@@ -42,24 +45,42 @@ export default class Drawer extends Component<DrawerProps, DrawerState> {
4245
openDrawer: () => null,
4346
closeDrawer: () => null,
4447
};
48+
private handleDrawer: (isOpen: boolean) => void;
4549
constructor(props: DrawerProps) {
4650
super(props);
51+
this.handleDrawer = (isOpen: boolean) => {
52+
isOpen ? this.openDrawer() : this.closeDrawer();
53+
};
4754
this.state = {
4855
zIndexValue: 0,
4956
overlayValue: new Animated.Value(0),
5057
drawerValue: new Animated.ValueXY({ ...this.getInitPosition() }),
58+
handleDrawer: this.handleDrawer,
59+
control: 'state',
60+
isOpen: !!props.isOpen,
5161
};
5262
}
63+
static getDerivedStateFromProps(props: DrawerProps, state: DrawerState) {
64+
if (state.control === 'state') {
65+
return {
66+
control: 'props',
67+
};
68+
}
69+
if (props.isOpen !== state.isOpen) {
70+
state.handleDrawer(!!props.isOpen);
71+
return {
72+
isOpen: props.isOpen,
73+
control: 'props',
74+
};
75+
}
76+
return null;
77+
}
5378
componentDidMount() {
5479
if (this.props.isOpen) {
5580
this.openDrawer();
5681
}
5782
}
58-
UNSAFE_componentWillReceiveProps(nextProps: DrawerProps) {
59-
if (nextProps.isOpen !== this.props.isOpen) {
60-
this.handleDrawer(!!nextProps.isOpen);
61-
}
62-
}
83+
6384
onOverlayClick = (e: GestureResponderEvent) => {
6485
const { maskClosable } = this.props;
6586
if (!maskClosable) {
@@ -140,9 +161,7 @@ export default class Drawer extends Component<DrawerProps, DrawerState> {
140161
</Fragment>
141162
);
142163
}
143-
handleDrawer(isOpen: boolean) {
144-
isOpen ? this.openDrawer() : this.closeDrawer();
145-
}
164+
146165
getInitPosition() {
147166
const { drawerWidth, placement, drawerHeight } = this.props;
148167
const xy = { x: 0, y: 0 };
@@ -161,7 +180,7 @@ export default class Drawer extends Component<DrawerProps, DrawerState> {
161180
return xy;
162181
}
163182
openDrawer() {
164-
this.setState({ zIndexValue: 3002 });
183+
this.setState({ zIndexValue: 3002, control: 'state' });
165184
Animated.parallel([
166185
Animated.spring(this.state.drawerValue, {
167186
toValue: { x: 0, y: 0 },
@@ -194,7 +213,7 @@ export default class Drawer extends Component<DrawerProps, DrawerState> {
194213
]).start(() => {
195214
this.props.closeDrawer!(false);
196215
this.props.onChange!(false);
197-
this.setState({ zIndexValue: 0 });
216+
this.setState({ zIndexValue: 0, control: 'state' });
198217
});
199218
}
200219
}

0 commit comments

Comments
 (0)