2024-11-03 18:18:07 -04:00
|
|
|
import { useState, useEffect, useRef } from 'react';
|
|
|
|
import { AppState, AppStateStatus } from 'react-native';
|
|
|
|
|
|
|
|
const useAppState = (): { currentAppState: AppStateStatus, previousAppState: AppStateStatus | null } => {
|
|
|
|
const [currentAppState, setCurrentAppState] = useState<AppStateStatus>(AppState.currentState);
|
|
|
|
const previousAppState = useRef<AppStateStatus | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleAppStateChange = (nextAppState: AppStateStatus) => {
|
|
|
|
previousAppState.current = currentAppState;
|
|
|
|
setCurrentAppState(nextAppState);
|
|
|
|
};
|
|
|
|
|
|
|
|
const subscription = AppState.addEventListener('change', handleAppStateChange);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
subscription.remove();
|
|
|
|
};
|
2024-11-05 16:07:28 -04:00
|
|
|
}, [currentAppState]);
|
2024-11-03 18:18:07 -04:00
|
|
|
|
|
|
|
return { currentAppState, previousAppState: previousAppState.current };
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useAppState;
|