Created
June 23, 2019 19:24
-
-
Save wcandillon/49a95eb6e0bf52a208adc62e87521865 to your computer and use it in GitHub Desktop.
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 React, { useEffect, useState } from "react"; | |
import { Asset } from "expo-asset"; | |
import { AppLoading } from "expo"; | |
export const usePromiseAll = <T extends any>( | |
promises: Promise<T>[], | |
cb: () => void | |
) => | |
useEffect(() => { | |
(async () => { | |
await Promise.all(promises); | |
cb(); | |
})(); | |
}); | |
export const useLoadAssets = (assets: number[]): boolean => { | |
const [ready, setReady] = useState(false); | |
usePromiseAll(assets.map(asset => Asset.loadAsync(asset)), () => | |
setReady(true) | |
); | |
return ready; | |
}; | |
interface LoadAssetsProps { | |
assets: number[]; | |
children: JSX.Element; | |
} | |
export default ({ children, assets }: LoadAssetsProps) => { | |
const ready = useLoadAssets(assets); | |
if (!ready) { | |
return <AppLoading />; | |
} | |
return children; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment