Skip to content

Instantly share code, notes, and snippets.

@wcandillon
Created June 23, 2019 19:24
Show Gist options
  • Save wcandillon/49a95eb6e0bf52a208adc62e87521865 to your computer and use it in GitHub Desktop.
Save wcandillon/49a95eb6e0bf52a208adc62e87521865 to your computer and use it in GitHub Desktop.
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