Last active
February 27, 2020 13:03
-
-
Save julioxavierr/d6c65e851382aa03fa6c4c0c9f980848 to your computer and use it in GitHub Desktop.
React hook to abstract loading resources from expo and showing splash screen until completion
This file contains 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, useState } from 'react'; | |
import * as Font from 'expo-font'; | |
import { SplashScreen } from 'expo'; | |
/** | |
* Load and use resources that need to be loaded async by Expo SDK | |
*/ | |
const useExpoResources = () => { | |
const [isLoading, setIsLoading] = useState(true); | |
/** | |
* Load resources and prevents SplashScreen from hiding until completed | |
*/ | |
useEffect(() => { | |
SplashScreen.preventAutoHide(); | |
const loadFonts = async () => { | |
try { | |
await Font.loadAsync({ | |
SomeFont: require('../../assets/fonts/SomeFont.ttf'), | |
}); | |
setIsLoading(false); | |
SplashScreen.hide(); | |
} catch (error) { | |
// handle error | |
} | |
}; | |
loadFonts(); | |
}, []); | |
return isLoading; | |
}; | |
export default useExpoResources; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment