18 lines
516 B
TypeScript
Raw Normal View History

import { useState } from 'react';
2021-02-20 17:07:10 +01:00
import { useEffect } from 'react';
import { AppState, AppStateStatus } from 'react-native';
2021-02-19 18:53:08 +01:00
export const useIsForeground = (): boolean => {
const [isForeground, setIsForeground] = useState(true);
2021-02-19 18:53:08 +01:00
useEffect(() => {
2021-02-22 12:28:56 +01:00
const onChange = (state: AppStateStatus): void => {
2021-02-20 17:07:10 +01:00
setIsForeground(state === 'active');
2021-02-19 18:53:08 +01:00
};
const listener = AppState.addEventListener('change', onChange);
return () => listener.remove();
2021-02-19 18:53:08 +01:00
}, [setIsForeground]);
return isForeground;
2021-02-20 17:07:10 +01:00
};