react-native-hcaptcha icon indicating copy to clipboard operation
react-native-hcaptcha copied to clipboard

BackHandler.removeEventListener is not a function

Open Bufib opened this issue 6 months ago • 5 comments

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!

Bufib avatar May 31 '25 17:05 Bufib

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 avatar May 31 '25 17:05 Bufib

@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 avatar Jun 01 '25 05:06 CAMOBAP

@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 avatar Jun 15 '25 10:06 goulashsoup

@goulashsoup thanks for sharing this.

  1. Based on 14.0.0-rc.1 I hope it's not dead completely.
  2. hCaptcha/react-native-hcaptcha doesn't strictly depend on react-native-modal. The HCaptcha component can be imported directly into your code and you can display it in the way you want (without react-native-modal).

CAMOBAP avatar Jun 15 '25 16:06 CAMOBAP

@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"
  },

goulashsoup avatar Jun 15 '25 20:06 goulashsoup