About DeviceEventEmitter.removeAllListeners()

12 August 2023
Avoid using it on Android platform
react-native

Today I learned (the hard way), that calling DeviceEventEmitter.removeAllListeners() will remove BackHandler's hardwareBackPress event as well.

The recommended way of removing listeners are removing the specific event listener only.

For example:

  React.useEffect(() => {
    const subscription = DeviceEventEmitter.addListener(eventName, handler);

    return () => subscription.remove();
  }, []);

Thanks for reading. Happy coding!