【问题标题】:How to navigate from splash screen to Onboarding Screens?如何从启动屏幕导航到入职屏幕?
【发布时间】:2022-01-06 23:29:18
【问题描述】:

这是 SplashScreen.js 页面

我想显示初始屏幕并在超时后不可见,然后导航到入职屏幕(滑动初始屏幕)

import React from 'react';
import { View} from 'react-native';

import LogoImage from './LogoImage.js'
import styles from '../stylesheets/SplashStylesheet.js'


const SplashScreen = ({ navigation }) => {
render() {
        let that = this;
        setTimeout(function(){that.setState({timePassed: true})}, 1000);
        const { navigate } = this.props.navigation;

        if (!this.state.timePassed){
           return (
          <View
                style = {styles.splashScreen}>
                <LogoImage/>
          </View>
        );
    }
    else{
        () => navigate('Onboarding);
    }

export default SplashScreen;

【问题讨论】:

    标签: javascript android reactjs native


    【解决方案1】:

    这样试试

    import React, { useState } from "react";
    import { View, Text, StyleSheet } from "react-native";
    
    const SplashScreen = ({ navigation }) => {
      const [timePassed, setTimePassed] = useState(false);
    
      setTimeout(function () {
        setTimePassed(true);
      }, 5000);
    
      if (!timePassed) {
        return (
          <View style={styles.splash}>
            <Text style={styles.text}>Splash Screen</Text>
          </View>
        );
      }
      navigation.navigate('Onboarding Screen');
      return null;
    };
    
    const styles = StyleSheet.create({
      splash: {
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        height: "100vh"
      },
      text: {
        fontSize: 20
      }
    });
    
    export default SplashScreen;
    

    我现在在导航发生时设置了警报。您可以将其更改为重定向。

    在您的App.js 中,您需要有一个stackNavigator 设置,如下所示,它提供了navigation 属性。

    import { NavigationContainer } from '@react-navigation/native';
    import { createStackNavigator } from '@react-navigation/stack';
    
    const Stack = createStackNavigator();
    
    const App = () => {
      return (
        <NavigationContainer>
          <Stack.Navigator initialRouteName="Splash Screen" >
            <Stack.Screen name="Splash Screen" component={SplashScreen} />
            <Stack.Screen name="Main Page" component={OnboadingPage} />
          </Stack.Navigator>
        </NavigationContainer>
      );
    };
    

    代码沙箱 => https://codesandbox.io/s/cool-violet-7lqqr?file=/src/App.js

    【讨论】:

    • @Hiruni Liyanage,看看这个!!
    • 谢谢,如何重定向到入职页面?
    • @HiruniLiyanage,我已经更新了答案!
    • 谢谢....你能检查一下吗! >>> stackoverflow.com/questions/70429822/…
    • @HiruniLiyanage,如果它解决了您的问题,您能否将其标记为答案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2020-03-06
    • 1970-01-01
    • 2022-01-20
    • 2014-12-07
    • 1970-01-01
    相关资源
    最近更新 更多