subscription returned by AppState.addEventListener() is void

Viewed 543

when i try to remove the eventListener like in the React native documentation for AppState: https://reactnative.dev/docs/appstate, I either get an error that there is no such function remove() on undefined since AppState.addEventListener() returns void or the eventListener is just not removed.

here is my code:

useEffect(() => {
    const subscription = AppState.addEventListener('change', (nextAppState: any) => {
      // some stuff 
    });

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

If anyone has an idea on that matter, i would be very grateful, thank you !

I found someone on github with the same issue I believe: https://github.com/facebook/react-native/issues/33151

2 Answers

ok, found the answer this due to the fact that im on react-native 0.64 and not 0.67

This happens if you're using a version of react-native prior to 0.65. Starting with 0.65, AppState.removeEventListener is deprecated in favor of using remove in the subscription returned by `addEventListener.

These are the 0.64 docs for removeEventListener. And these are the corresponding 0.65 docs that add the deprecation notice.

Related