From b7e050c9abab17b9a3ac21afed5bc73cecbecd6d Mon Sep 17 00:00:00 2001 From: David Prevost <77302423+dprevost-LMI@users.noreply.github.com> Date: Fri, 21 Feb 2025 06:50:07 -0500 Subject: [PATCH] fix: Removal of `BackHandler.removeEventListener` in react-native (#339) --- src/ActionSheet/CustomActionSheet.tsx | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ActionSheet/CustomActionSheet.tsx b/src/ActionSheet/CustomActionSheet.tsx index e88906c..e821158 100644 --- a/src/ActionSheet/CustomActionSheet.tsx +++ b/src/ActionSheet/CustomActionSheet.tsx @@ -4,6 +4,7 @@ import { BackHandler, Easing, Modal, + NativeEventSubscription, Platform, StyleSheet, TouchableWithoutFeedback, @@ -38,6 +39,7 @@ const ESCAPE_KEY = 'Escape'; // Has same API as https://facebook.github.io/react-native/docs/actionsheetios.html export default class CustomActionSheet extends React.Component { _actionSheetHeight = 360; + _backHandlerListener: NativeEventSubscription | undefined; state: State = { isVisible: false, @@ -227,8 +229,12 @@ export default class CustomActionSheet extends React.Component { this._deferAfterAnimation = undefined; } }); - // @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"' - BackHandler.addEventListener('actionSheetHardwareBackPress', this._selectCancelButton); + + this._backHandlerListener = BackHandler.addEventListener( + // @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"' + 'actionSheetHardwareBackPress', + this._selectCancelButton + ); }; _selectCancelButton = () => { @@ -269,8 +275,9 @@ export default class CustomActionSheet extends React.Component { return false; } - // @ts-ignore: Argument of type '"actionSheetHardwareBackPress"' is not assignable to parameter of type '"hardwareBackPress"' - BackHandler.removeEventListener('actionSheetHardwareBackPress', this._selectCancelButton); + if (this._backHandlerListener) { + this._backHandlerListener.remove(); + } this.setState({ isAnimating: true, });