Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apploading splashscreen with hooks

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

like image 499
Bill Avatar asked Jul 10 '26 07:07

Bill


1 Answers

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>)
  );
}


like image 189
hong developer Avatar answered Jul 13 '26 16:07

hong developer