react-native-vision-camera/example/src/hooks/useIsForeground.ts

20 lines
539 B
TypeScript
Raw Normal View History

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 => {
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
};
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
};