2021-05-11 12:59:05 +02:00
|
|
|
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 => {
|
2021-05-11 12:59:05 +02:00
|
|
|
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
|
|
|
};
|
2021-09-22 08:25:15 -03:00
|
|
|
const subscription = AppState.addEventListener('change', onChange);
|
|
|
|
return () => {
|
|
|
|
subscription.remove();
|
|
|
|
};
|
2021-02-19 18:53:08 +01:00
|
|
|
}, [setIsForeground]);
|
|
|
|
|
|
|
|
return isForeground;
|
2021-02-20 17:07:10 +01:00
|
|
|
};
|