This commit is contained in:
Marc Rousavy
2021-02-19 18:53:08 +01:00
parent c13c46f388
commit cf157cb299
8 changed files with 289 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
import { useEffect } from "react"
import { AppState, AppStateStatus } from "react-native";
import { useCachedState } from "./useCachedState";
export const useIsForeground = (): boolean => {
const [isForeground, setIsForeground] = useCachedState(true);
useEffect(() => {
const onChange = (state: AppStateStatus) => {
setIsForeground(state === "active");
};
AppState.addEventListener("change", onChange);
return () => AppState.removeEventListener("change", onChange);
}, [setIsForeground]);
return isForeground;
}