Skip to content

Instantly share code, notes, and snippets.

@mauriciord
Forked from julioxavierr/useExpoResources.js
Created February 27, 2020 13:03
Show Gist options
  • Save mauriciord/0a28c42ff2ec43818c7caac02c6a3e1d to your computer and use it in GitHub Desktop.
Save mauriciord/0a28c42ff2ec43818c7caac02c6a3e1d 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