React Native StackNavigator initialRouteName

∥☆過路亽.° 提交于 2021-02-07 05:13:04

问题


In React Native Navigation library 'react-navigation'

How could I set StackNavigator initialRouteName by AsyncStorage?

function getInitialScreen() {
    AsyncStorage.getItem('initialScreen')
        .then(screenName => {
            return (screenName)
                ? screenName
                : 'Login';
        })
        .catch(err => {});
}

const Navigator = StackNavigator({
    Splash: { screen: Splash },
    Login: { screen: Login },
    WebPage: { screen: WebPage }
}, {
    initialRouteName: getInitialScreen()
});

回答1:


Changing InitialRouteName with multiple Route depending upon your requirement. I have got it working this way.

create router file import all your screens.

export a stateless function call it createRootNavigator with params as (load="<Your initial screen>")

export const createRootNavigator = (load="<Your Initial Screen>") => {
  return stackNavigator({
     Initialize: {
       screen: Initialize,
     },
     Main: {
      screen: Main,
     },
     { 
       initialRouteName: load
     }
  })
}

In your main app,

state = {
  load: "<Your Initial Screen>"
}

eg:

state = {
   load: "Initialize" // value is string
}

Set the state accordingly in componentDidMount() method. And finally render new layout.

render() {
  const Layout = createRootNavigator(this.state.load);
  <Layout />
}

The above method worked fine for me. Hope it helps somebody.




回答2:


I’ve also had this problem and currently the only good solution is the following example:

 const RootNavLogged = StackNavigator({
     ...
  },{
     initialRouteName : 'Home'
  });

  const RootNav = StackNavigator({
     ...
  },{
     initialRouteName : 'Login'
  });

  class App extends Component {
     render(){
         if (this.props.userLogged == true ){
            return (
               <RootNavLogged/>
            ) 
          } else {
             return(
                <RootNav/>
              ) 
          }
     }
 }



回答3:


Full Solution from React Native Navigation on Restart:

const Navigator = StackNavigator({
    InitialScreen: {
        screen: InitialScreen
    },
    Splash: {
        screen: Splash
    },
    LanguageStartup: {
        screen: LanguageStartup
    },
    Login: {
        screen: Login
    },
    Register: {
        screen: Register
    }
}, {initialRouteName: 'InitialScreen'});

export default Navigator;

My Initial Screen

import React, {Component} from 'react';
import {connect} from 'react-redux';
import * as GeneralPref from './../preferences/GeneralPref'
import Log from './../utils/Log'
import {AsyncStorage, View} from 'react-native';
import * as Pref from './../preferences/Preferences';
import {NavigationActions} from 'react-navigation'

const TAG = 'InitialScreen'

class InitialScreen extends Component {
    static navigationOptions = {
        header: false
      };
    componentWillMount() {
        Log(TAG+' Mount')
        const {navigate} = this.props.navigation;
        GeneralPref
            .getInitialScreen()
            .then(value => {
                Log(TAG+' Initial',value)                
                if (value != null) {
                    Log(TAG+' Initial',value)                                    
                    return value
                } else {
                    Log(TAG+' No Initial','Splash')                                    
                    return 'Splash'
                }
            })
            .then(screenName => this.props.navigation.dispatch(NavigationActions.reset({
                index: 0,
                actions: [NavigationActions.navigate({routeName: screenName})]
            })))
            .catch(err => {
                Log(TAG+' Initial Error',value)                                
                this.props.navigation.dispatch(NavigationActions.reset({
                    index: 0,
                    actions: [NavigationActions.navigate({routeName: 'Splash'})]
                }))
            });
    }
    render() {
        return null;
    }
}

export default InitialScreen;

then in Language Screen

changeLanguageTo(language) {
    Log(TAG+'Change Language', "Change Language To: " + language.code);
    // Log(TAG, 'Current State');
    Log(TAG+' Language State', language);
    GeneralPref.setInitialScreen('Login');

    this
      .props
      .actions
      .changeLanguage(language);
      I18nManager.forceRTL(true);      
    // Immediately reload the React Native Bundle
    RNRestart.Restart();
  };


来源:https://stackoverflow.com/questions/46684007/react-native-stacknavigator-initialroutename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!