BackHandler.removeEventListener is not a function
Hey,
I use Expo SDK 53 with hcaptcha and my app gives me the error "Warning: TypeError: _reactNative.BackHandler.removeEventListener is not a function (it is undefined)".
Using chatGPT it suggests that the problem is coming from the following:
The stack trace shows that the offending call to BackHandler.removeEventListener("hardwareBackPress", …) is coming from inside ReactNativeModal (which @hcaptcha/react-native-hcaptcha depends on). In RN 0.71+ (and Expo SDK 48+), BackHandler.removeEventListener no longer exists, so any library that still calls it will blow up.
Are there any updates on this?
Thank you!
This patch in my root works but is there something official?
// If removeEventListener doesn’t exist, patch it on-the-fly:
if (typeof (BackHandler as any).removeEventListener !== "function") {
;(BackHandler as any).removeEventListener = (eventName: any, handler: () => boolean) => {
// Create a dummy subscription and immediately remove it.
const subscription = BackHandler.addEventListener(eventName, handler);
subscription.remove();
};
}
@Bufib thanks for reporting
You correctly identified the issue.
Can you confirm that workaround https://github.com/react-native-modal/react-native-modal/issues/807 is works for you?
@CAMOBAP react-native-modal had its last release in 2022, in software this is basically dead, I think you should get rid of that dependency, it will just lead to more problems in the future otherwise.
@goulashsoup thanks for sharing this.
- Based on
14.0.0-rc.1I hope it's not dead completely. hCaptcha/react-native-hcaptchadoesn't strictly depend onreact-native-modal. TheHCaptchacomponent can be imported directly into your code and you can display it in the way you want (withoutreact-native-modal).
@CAMOBAP Thanks for your feedback, I haven't checked the code, just the package.json:
"peerDependencies": {
"react": "*",
"react-native": "*",
"react-native-modal": ">=11.5.4",
"react-native-webview": ">=9.0.1"
},