2021-05-11 04:59:05 -06:00
|
|
|
import { useState } from 'react';
|
2021-02-20 09:07:10 -07:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { AppState, AppStateStatus } from 'react-native';
|
2021-02-19 10:53:08 -07:00
|
|
|
|
|
|
|
export const useIsForeground = (): boolean => {
|
2021-05-11 04:59:05 -06:00
|
|
|
const [isForeground, setIsForeground] = useState(true);
|
2021-02-19 10:53:08 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-22 04:28:56 -07:00
|
|
|
const onChange = (state: AppStateStatus): void => {
|
2021-02-20 09:07:10 -07:00
|
|
|
setIsForeground(state === 'active');
|
2021-02-19 10:53:08 -07:00
|
|
|
};
|
2021-09-22 05:25:15 -06:00
|
|
|
const subscription = AppState.addEventListener('change', onChange);
|
|
|
|
return () => {
|
|
|
|
subscription.remove();
|
|
|
|
};
|
2021-02-19 10:53:08 -07:00
|
|
|
}, [setIsForeground]);
|
|
|
|
|
|
|
|
return isForeground;
|
2021-02-20 09:07:10 -07:00
|
|
|
};
|