Si revisa el ejemplo en la documentación y modifica su useEffect a:
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(); }; }, []);(prueba este bocadillo )
puede ver que .remove() no es una propiedad de void (como se esperaba). Además, después de actualizar a la última versión nativa de reacción (0.68.2) , recibo la advertencia
EventEmitter.removeListener('appStateDidChange', ...): el método ha quedado obsoleto. En su lugar, use
remove()en la suscripción devuelta porEventEmitter.addListener.
mientras que removeEventListener no está marcado como @deprecated en el núcleo y addEventListener devuelve 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; }¿Me estoy perdiendo de algo?
Ver también: https://github.com/facebook/react-native/issues/33151