2024-02-03 20:30:00 -07:00
|
|
|
import { useEffect, useState } from "react";
|
2024-02-03 20:23:31 -07:00
|
|
|
import { AppState, AppStateStatus } from "react-native";
|
2024-01-30 15:08:46 -07:00
|
|
|
|
|
|
|
export const useIsForeground = (): boolean => {
|
2024-02-03 20:23:31 -07:00
|
|
|
const [isForeground, setIsForeground] = useState(true);
|
2024-01-30 15:08:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
useEffect(() => {
|
|
|
|
const onChange = (state: AppStateStatus): void => {
|
|
|
|
setIsForeground(state === "active");
|
|
|
|
};
|
|
|
|
const listener = AppState.addEventListener("change", onChange);
|
|
|
|
return () => listener.remove();
|
|
|
|
}, [setIsForeground]);
|
2024-01-30 15:08:46 -07:00
|
|
|
|
2024-02-03 20:23:31 -07:00
|
|
|
return isForeground;
|
|
|
|
};
|