简体   繁体   English

如何使用 expo 在本机构建中隐藏启动画面

[英]How to hide splash screen in react native build using expo

My app is not loading after splash screen.我的应用程序在启动画面后未加载。 It just stuck there.它只是停留在那里。 So I added expo-splash-screen and still is not passing the splash screen.所以我添加了 expo-splash-screen 但仍然没有通过启动画面。 Before adding splash screen everything was working fine:(在添加启动画面之前一切正常:(

See this is my App.js code.看到这是我的 App.js 代码。 As you can see it only holds the navigation container which holds the links to other screens including the main home screen.如您所见,它仅包含导航容器,该容器包含指向其他屏幕(包括主屏幕)的链接。

import {StatusBar } from 'expo-status-bar';
import { StyleSheet } from 'react-native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { MaterialIcons } from "@expo/vector-icons";
import { HomeNavigator } from './CustomNavigation';
import * as Font from 'expo-font';
import * as SplashScreen from 'expo-splash-screen';

import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import FavouritesScreen from './src/screens/FavouritesScreen'
import HomeScreen from './src/screens/HomeScreen';
import MoreOptions from './src/screens/MoreOptions'
import React, { useEffect, useState } from 'react'
console.reportErrorsAsExceptions = false; //to hide touch warning
const Stack = createNativeStackNavigator()

const Tab = createBottomTabNavigator();
 
SplashScreen.preventAutoHideAsync();

 
export default function App() {
  const [fontLoaded, setFontLoaded] = useState(false)
  const [appIsReady, setAppIsReady] = useState(false);

  useEffect(() => {
     async function prepare() {
        try {
 
        await Font.loadAsync(Entypo.font);
      
        await new Promise(resolve => setTimeout(resolve, 2000));
      } catch (e) {
        console.warn(e);
      } finally {
        // Tell the application to render
        setAppIsReady(true);
      }
    }

    prepare();
  }, []);
 const onLayoutRootView = useCallback(async () => {
    if (appIsReady) {
    
      await SplashScreen.hideAsync();
    }
  }, [appIsReady]);

  if (!appIsReady) {
    return null;` `
  }

  return (
    <View
      style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}
      onLayout={onLayoutRootView}>
 <StatusBar barStyle="dark-content" hidden={false} backgroundColor="#ff3b3b" translucent={true} />
      <NavigationContainer>
    //it contains nothing but the navigation code
     </NavigationContainer>

</View>)

Please tell me where and what I'm doing wrong here.请告诉我我在哪里做错了什么。

I give you the working example please check it我给你工作示例请检查它
then after you don't understand let me know然后在你不明白的时候告诉我

code代码

import React, { useRef, useEffect } from 'react';
import { StyleSheet, View, Image, StatusBar, Text } from 'react-native';

/**
 * RN libraries
 */
import { useNavigation } from '@react-navigation/native';

export default function Splash() {
  const animation = useRef(null);
  const navigation = useNavigation();

  useEffect(() => {
    animation.current?.play();

    navigation.addListener('focus', () => {
      animation.current?.play();
    });

    setTimeout(() => {
      navigate();
    }, 2500);
  }, []);

  const navigate = () => {
    navigation.navigate('Home');
  };

  return (
    <View style={styles.animationContainer}>
      <Text
        style={{
          textAlign: 'center',
          fontSize: 50,
          fontStyle: 'italic',
          fontFamily: 'Poppins-Medium',
          fontWeight: '800',
          color: '#fff',
        }}>
        Online{'\n'}Education
      </Text>
      <Text
        style={{ color: '#fff', fontSize: 20, fontFamily: 'Poppins-Medium' }}>
        Free{'\n'}
      </Text>
      <View
        style={{
          backgroundColor: '#5D6CFA',
          width: 169,
          height: 117,
          borderRadius: 60,
          flexDirection: 'row',
          alignItems: 'center',
          justifyContent: 'center',
        }}>
        {/* <Text style={{color:"#000",fontFamily:'Poppins-Bold',fontSize:25}}>Let's start</Text> */}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  animationContainer: {
    backgroundColor: '#000',
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1,
  },
});

Maybe pass your setAppIsReady(true);也许通过你的 setAppIsReady(true); after the finally and then remove it like this在 finally 之后然后像这样删除它

React.useEffect(() => {
   async function prepare() {
     try {
       // Pre-load fonts, make any API calls you need to do here
       AsyncStorage.setItem("alreadyAppLaunched", "true")
       await LoadFonts()
       await checkOsDarkMode()
       await stores.AuthStore.getAllData()
     } catch (e) {
       console.warn(e);
     }
     setReady(true);
   }
   prepare()
 }, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM