react-native-modal: BackHandler.removeEventListener is not a function in React Native 0.77

πŸ› Bug Report Since updating to React Native 0.77, BackHandler.removeEventListener is no longer available. The new API uses BackHandler.remove(), which causes an error in react-native-modal when attempting to remove the event listener.

⚠️ Error Message: TypeError: _reactNative.BackHandler.removeEventListener is not a function (it is undefined)

πŸ“ Affected Version: React Native: 0.77+ react-native-modal: 13.0.1

πŸ’‘ Steps to Reproduce: Install react-native-modal in a React Native 0.77 project. Open a modal that listens for the back button. Close the modal or navigate away. The app crashes with the error.

πŸ” Expected Behavior: The modal should correctly remove the BackHandler event listener without crashing.

πŸ› οΈ Suggested Fix: Replace: BackHandler.removeEventListener("hardwareBackPress", this.handleBackButton);

With: BackHandler.remove("hardwareBackPress", this.handleBackButton);

A similar fix was applied in PR #761 after React Native removed removeEventListener.

πŸ“Ž References: React Native 0.77 Breaking Changes GitHub PR with Fix Would appreciate a fix or official workaround. Thanks! πŸ™

Image

CC: @ancyrweb @mmazzarolo

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 21
  • Comments: 18 (3 by maintainers)

Most upvoted comments

I would love to avoid a patch if possible - can #784 get merged?

not possible, this package is no longer mantained. just patch.

Why you guys disliked it?

Npm says this package is no longer maintaining.

Just one version that was published by mistake. I’m considering rewriting this package from scratch for v15.0.0 because the code as it is today is so difficult to maintain.

I would love to avoid a patch if possible - can https://github.com/react-native-modal/react-native-modal/pull/784 get merged?

@codeion landing in 14.0.0-rc.1 πŸ˜ƒ

I targetted 0.76 instead of 77+ because in the following versions, removeEventListener has been removed from the BackHandler interface.

And it seems Expo is still targetting 0.76 anyway.

I would love to avoid a patch if possible - can #784 get merged?

not possible, this package is no longer mantained. just patch.

Is there any reason this bug is closed with an awaiting open PR required by the react-native folks for Android 15?

Since the library works fine on the web, would you consider removing the unnecessary error log from react-native-web? The error message β€œBackHandler is not supported on web and should not be used.” clutters the console, but it doesn’t affect functionality.

A simple fix would be to conditionally import BackHandler only on native platforms:

const BackHandler = Platform.OS === 'web'
  ? { exitApp: () => {}, addEventListener: () => ({ remove: () => {} }), removeEventListener: () => {} }
  : require('react-native').BackHandler;

This prevents the warning while maintaining expected behavior on all platforms. Thank you for considering this improvement!

Sorry the for inconvenience, I took back the package and upgraded it to the latest version. Please upgrade to 14.0.0-rc.0 see if it works correctly.

@maksymhcode-care Just saw this the minute you posted it, tested it now and it works like a charm! Thank you!