3

How do you achieve loading resources while showing the splash screen when you are using functional components with hooks? What is the pattern in using apploading and/or splashscreen with hooks?

Thanks!

Bill

Bill
  • 512
  • 4
  • 14
  • Please provide a minimal reproducible example – hong developer Aug 29 '19 at 13:05
  • In the expo documentation, they recommended using the apploading component. The example they gave is using class based component (please see https://docs.expo.io/versions/latest/sdk/app-loading/). How do I use this with react hooks? – Bill Aug 29 '19 at 13:08

1 Answers1

1

If you only understand Hook's useState, this is a very easy change. This is simply converted into a function, and the state value is resolved using hooks. If you change the example of AppLoading to Hook, the code below is as follows.

AppLoading use Hooks

import React, { useState } from 'react';
import { View ,Image } from "react-native";
import { Asset } from 'expo-asset';
import { AppLoading } from 'expo';

export default function App() {
  const [isReady, setReady] = useState(false);

  const  _cacheResourcesAsync = async () => {
    const images = [require('./assets/snack-icon.png')];

    const cacheImages = images.map(image => {
      return Asset.fromModule(image).downloadAsync();
    }); 
    return Promise.all(cacheImages);
  }

  return (
     isReady === false ? ( <AppLoading
      startAsync={_cacheResourcesAsync}
      onFinish={() => setReady(true)}
      onError={console.warn}
    />) : (<View style={{ flex: 1 }}>
      <Image source={require('./assets/snack-icon.png')} />
    </View>)
  );
}


hong developer
  • 13,291
  • 4
  • 38
  • 68