Skip to content

Commit b7e050c

Browse files
authored
fix: Removal of BackHandler.removeEventListener in react-native (#339)
1 parent 1ce802b commit b7e050c

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/ActionSheet/CustomActionSheet.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BackHandler,
55
Easing,
66
Modal,
7+
NativeEventSubscription,
78
Platform,
89
StyleSheet,
910
TouchableWithoutFeedback,
@@ -38,6 +39,7 @@ const ESCAPE_KEY = 'Escape';
3839
// Has same API as https://facebook.github.io/react-native/docs/actionsheetios.html
3940
export default class CustomActionSheet extends React.Component<Props, State> {
4041
_actionSheetHeight = 360;
42+
_backHandlerListener: NativeEventSubscription | undefined;
4143

4244
state: State = {
4345
isVisible: false,
@@ -227,8 +229,12 @@ export default class CustomActionSheet extends React.Component<Props, State> {
227229
this._deferAfterAnimation = undefined;
228230
}
229231
});
230-
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
231-
BackHandler.addEventListener('actionSheetHardwareBackPress', this._selectCancelButton);
232+
233+
this._backHandlerListener = BackHandler.addEventListener(
234+
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
235+
'actionSheetHardwareBackPress',
236+
this._selectCancelButton
237+
);
232238
};
233239

234240
_selectCancelButton = () => {
@@ -269,8 +275,9 @@ export default class CustomActionSheet extends React.Component<Props, State> {
269275
return false;
270276
}
271277

272-
// @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"'
273-
BackHandler.removeEventListener('actionSheetHardwareBackPress', this._selectCancelButton);
278+
if (this._backHandlerListener) {
279+
this._backHandlerListener.remove();
280+
}
274281
this.setState({
275282
isAnimating: true,
276283
});

0 commit comments

Comments
 (0)