import { useState } from 'react'; import { useEffect } from 'react'; import { AppState, AppStateStatus } from 'react-native'; export const useIsForeground = (): boolean => { const [isForeground, setIsForeground] = useState(true); useEffect(() => { const onChange = (state: AppStateStatus): void => { setIsForeground(state === 'active'); }; const subscription = AppState.addEventListener('change', onChange); return () => { subscription.remove(); }; }, [setIsForeground]); return isForeground; };