λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

πŸšΆπŸ»μ–΄λ””λ‘œ κ±·κ³ μžˆλ‹ˆ?

🚢🏻#24 [React & RN] - 앱이 μ‹€ν–‰λ˜κΈ° μ „ Preload ν•˜λŠ” 방법

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 λ₯Ό μ‚¬μš©ν•˜μ—¬ λΆˆλŸ¬μ˜€λŠ” 방법이닀
  • λ‘œλ”© ν•¨μˆ˜λ₯Ό μ‚¬μš©ν• μˆ˜ μ—†μ–΄μ„œ κ°„λ‹¨ν•˜κ²Œ ν°νŠΈμ™€ 이미지λ₯Ό λΆˆλŸ¬μ˜¬λ•Œ μ‚¬μš©ν•˜λ©΄ 쒋을것같닀