Apploading
/* μ€μΉλ² */
expo install expo-app-loading
/* μ¬μ©λ² */
import AppLoading from 'expo-app-loading';
- RN μ κΈ°λ³Έμ μΌλ‘ μ±μ΄ λ‘λ©μ€ μΌλ splash μ΄λ―Έμ§λ₯Ό 보μ¬μ€λ€
- Apploading μ μ¬μ©νλ©΄ μ±μ΄ μ€νλκΈ° μ μ font, image, μ± μ€ν μ νμν DB μ 보 λ±λ± νμνκ²λ€μ λΆλ¬μ¬μ μλ€
Case 01 - Apploading Props μ¬μ©νκΈ°
import AppLoading from "expo-app-loading";
import React, { useState } from "react";
import * as Font from "expo-font";
import { Text, Image } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { Asset } from "expo-asset";
export default function App() {
const [ready, setReady] = useState(false);
const onFinish = () => setReady(true);
const startLoading = async () => {
await Font.loadAsync(Ionicons.font);
await Asset.loadAsync(require("./my-face.jpeg"));
await Image.prefetch("https://reactnative.dev/img/oss_logo.png");
};
if (!ready) {
return (
<AppLoading
startAsync={startLoading}
onFinish={onFinish}
onError={console.error}
/>
);
}
return <Text>We are done loading!</Text>;
}
- Apploading Component λ startAsync, onFinish, onError 3κ°μ§ Props λ₯Ό κ°μ§κ³ μλ€
- startAsync μ Type μ () => Promise<void> μ΄λ©°, startAsync κ° μλ£λκ³ λλ©΄ onFinish κ° μ€νλλ€
- μ μμ€λ₯Ό μλ‘ λ€λ©΄ async startLoading() λ΄λΆμ μλ κ°κ°μ await λ€μ΄ λ€ μλ£ λλ©΄, onFinish() λ΄λΆμ setReady(true) λ‘ state κ°μ λ³κ²½ν΄μ£Όλ©° μ± λ‘λ©μ΄ μλ£λλ€
Case 02 - λΆλ¬μμΌν ν°νΈκ° λ§λ€λ©΄?
import AppLoading from "expo-app-loading";
import React, { useState } from "react";
import * as Font from "expo-font";
import { Text, Image } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { Asset } from "expo-asset";
export default function App() {
const [ready, setReady] = useState(false);
const onFinish = () => setReady(true);
const loadFonts = (fonts) => fonts.map((font)=> Font.loadAsync(font));
const loadImages = (images) => images.map((image)=> Asset.loadAsync(image));
const startLoading = async () => {
const fonts = loadFonts([Ionicons.font]);
const images = loadImages([
require("./my-face.jpeg"),
"https://reactnative.dev/img/oss_logo.png",
]);
await Promise.all([...fonts, ...images]);
};
if (!ready) {
return (
<AppLoading
startAsync={startLoading}
onFinish={onFinish}
onError={console.error}
/>
);
}
return <Text>We are done loading!</Text>;
}
- 미리 λΆλ¬μ¬ ν°νΈμ μ΄λ―Έμ§ λ°°μ΄μ νλΌλ―Έν°λ‘ λ°λ ν¨μ loadFonts(), loadImages() λ₯Ό λ§λ λ€
- Case 01 μμλ λΆλ¬μμΌν κ²λ€ κ°κ°μ await λ₯Ό μΆκ°ν΄μ€¬μ§λ§, μ΄λ²μ await Promise.all() μ μ¬μ©νμ¬ νμν κ²λ€μ λΆλ¬μ¨λ€
- Promise.all() ν¨μλ μΈμκ°μΌλ‘ λ°°μ΄μ λ°λλ€
Case 03 - react hook μ¬μ©νκΈ°
import React from "react";
import AppLoading from "expo-app-loading";
import * as Font from "expo-font";
import { Text } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { useAssets } from "expo-asset";
export default function App() {
const [assets] = useAssets([require("./my-face.jpeg")]);
const [loaded] = Font.useFonts(Ionicons.font);
if(!assets || !loaded){
return <Apploading />;
}
return <Text>We are done loading!</Text>;
}
- useAssets μ useFonts λ₯Ό μ¬μ©νμ¬ λΆλ¬μ€λ λ°©λ²μ΄λ€
- λ‘λ© ν¨μλ₯Ό μ¬μ©ν μ μμ΄μ κ°λ¨νκ² ν°νΈμ μ΄λ―Έμ§λ₯Ό λΆλ¬μ¬λ μ¬μ©νλ©΄ μ’μκ²κ°λ€