diff --git a/native/utils/hooks.js b/native/utils/hooks.js new file mode 100644 --- /dev/null +++ b/native/utils/hooks.js @@ -0,0 +1,29 @@ +// @flow + +import AsyncStorage from '@react-native-async-storage/async-storage'; +import * as React from 'react'; + +function useOnFirstLaunchEffect(uniqueKey: string, effect: () => mixed) { + const [started, setStarted] = React.useState(false); + + React.useEffect(() => { + (async () => { + if (started) { + return; + } + setStarted(true); + const hasBeenExecuted = await AsyncStorage.getItem(uniqueKey); + if (hasBeenExecuted) { + return; + } + + try { + await effect(); + } finally { + await AsyncStorage.setItem(uniqueKey, 'true'); + } + })(); + }, [effect, started, uniqueKey]); +} + +export { useOnFirstLaunchEffect };