Created
March 10, 2023 12:15
-
-
Save iampato/6634a132a5429dfcaf8b081bd321ef82 to your computer and use it in GitHub Desktop.
Detect the app state in react native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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