Skip to content

Instantly share code, notes, and snippets.

@iampato
Created March 10, 2023 12:15
Show Gist options
  • Save iampato/6634a132a5429dfcaf8b081bd321ef82 to your computer and use it in GitHub Desktop.
Save iampato/6634a132a5429dfcaf8b081bd321ef82 to your computer and use it in GitHub Desktop.
Detect the app state in react native
import { useEffect, useRef } from 'react';
import { AppState } from 'react-native';
const getAppCurrentState = () => {
const appState = useRef(AppState.currentState);
useEffect(() => {
const subscription = AppState.addEventListener('change', nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!')
}
appState.current = nextAppState;
});
return () => subscription.remove();
}, []);
};
export default getAppCurrentState;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment