If you check the example in the documentation and modify its useEffect to:
useEffect(() => {
const subscription = AppState.addEventListener("change", nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
console.log("AppState", appState.current);
});
console.log(typeof subscription.remove); // <--- ERROR!
return () => {
subscription.remove();
};
}, []);
(test this snack)
you can see .remove() is not a property of void (as expected). Also, after upgrading to the last react native version (0.68.2), I am getting the warning
EventEmitter.removeListener('appStateDidChange', ...): Method has been deprecated. Please instead use
remove()on the subscription returned byEventEmitter.addListener.
while removeEventListener is not marked as @deprecated in the core, and addEventListener returns void.
export interface AppStateStatic {
currentState: AppStateStatus;
/**
* Add a handler to AppState changes by listening to the change event
* type and providing the handler
*/
addEventListener(type: AppStateEvent, listener: (state: AppStateStatus) => void): void;
/**
* Remove a handler by passing the change event type and the handler
*/
removeEventListener(type: AppStateEvent, listener: (state: AppStateStatus) => void): void;
}
Am I missing something?
Also see: https://github.com/facebook/react-native/issues/33151