Skip to content

Instantly share code, notes, and snippets.

@julioxavierr
Last active February 27, 2020 13:03
Show Gist options
  • Save julioxavierr/d6c65e851382aa03fa6c4c0c9f980848 to your computer and use it in GitHub Desktop.
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
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