Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React Native useAppState hook
import { useState, useEffect, useCallback } from 'react';
import { AppState, AppStateStatus } from 'react-native';
const useAppState = () => {
const [appState, setAppState] = useState(AppState.currentState);
const handleAppStateChange = useCallback(
(nextAppState: AppStateStatus) => {
setAppState(nextAppState);
},
[setAppState],
);
useEffect(() => {
AppState.addEventListener('change', handleAppStateChange);
return () => AppState.removeEventListener('change', handleAppStateChange);
}, [appState, handleAppStateChange]);
return appState;
};
export default useAppState;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment