Created
July 25, 2022 17:45
-
-
Save celian-rib/ba027919970b74cb0b1bd642fa21af99 to your computer and use it in GitHub Desktop.
React Native useEffect that takes effect only when app is in foreground
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'; | |
/** | |
* Accepts a function that contains imperative, possibly effectful code. | |
* Same as useEffect but has not effect when app is in background | |
* | |
* If the depencies changes while being in background, the effect will be executed after the app is in foreground again | |
* | |
* @param {*} effect Imperative function that can return a cleanup function | |
* @param {*} deps If present, effect will only activate if the values in the list change. | |
*/ | |
const useEffectForegroundOnly = (effect, deps) => { | |
const appState = useRef(AppState.currentState); | |
const needRefresh = useRef(false); | |
useEffect(() => { | |
const subscription = AppState.addEventListener('change', nextAppState => { | |
appState.current = nextAppState; | |
if(nextAppState === 'active' && needRefresh.current) { | |
effect(); | |
needRefresh.current = false; | |
} | |
}); | |
return () => subscription.remove(); | |
}, []); | |
useEffect(() => { | |
if (appState.current === 'active') { | |
effect(); | |
needRefresh.current = false; | |
} else { | |
needRefresh.current = true; | |
} | |
}, deps); | |
}; | |
export default useEffectForegroundOnly; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment