【问题标题】:How to change SwitchNavigator screen dynamically如何动态更改 SwitchNavigator 屏幕
【发布时间】:2018-09-13 16:53:40
【问题描述】:

所以我有一个 SwitchNavigator,它在一个函数中使用,该函数接收一个值作为道具(加载真或假)。我想根据firebase onAuthStateChange 更改该功能道具,因此如果我没有收到响应,我会渲染一个微调器,如果我收到一个我会更改状态并将其用作函数但现在这不起作用,因为componentDidMount 没有等待来自firebase功能的回应!有什么想法吗?

import React from 'react';
    import { StyleSheet, Text, View } from 'react-native';
    import { createStackNavigator, SwitchNavigator } from 'react-navigation';
    import MainScreen from './MainScreen';
    import AuthenticationNav from './Authentication';
    import firebase from 'firebase';
    import SplashScreen from './SplashScreen';

    export default class App extends React.Component {
      constructor(){
        super();
        this.state={
          screen:'loading'
        }
      }
      componentWillMount() {
      }
      componentDidMount(){
        let isItTrue
        firebase.auth().onAuthStateChanged(function(user) {
            if (user) {
             isItTrue=true;
            } else {
              isItTrue=false
            }
          });
          this.setState({screen:isItTrue})
      }
      render() {console.log(this.state)
        const App=app(this.state.screen)
        return (
            <View style={{ flex: 1 }}>
              <App />
            </View>
        );
      }
    }
    const Main = createStackNavigator({
      Main: {
        screen: MainScreen
      }
    })
    const Authentication = createStackNavigator({
      AuthenticationScreen: {
        screen: AuthenticationNav
      }
    }, {
        headerMode: 'screen'
      })
    const Splash = createStackNavigator({
      SplashScreen: {
        screen: SplashScreen
      }
    })
    const app = (isSignedIn) => {
      return SwitchNavigator({
        SignedIn: {
          screen: Main
        },
        SignedOut: {
          screen: Authentication
        },
        Loading:{
          screen:Splash
        }
      },
        {
          initialRouteName: isSignedIn==='loading' ? "Loading" : isSignedIn?"SignedIn":"SignedOut"
        }
      )
    }

【问题讨论】:

    标签: reactjs firebase react-native redux react-navigation


    【解决方案1】:

    欢迎弗拉德!

    首先,here's a guide from react-navigation 给出了使用 SwitchNavigator 的示例。你可能已经看到了。

    我会移动逻辑来确定是否登录到 Splash 组件,如下所示:

    // SplashScreen.js
    componentDidMount(){
      firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
          this.props.navigation.navigate('SignedIn');
        } else {
          this.props.navigation.navigate('SignedOut');
        }
      });
    }

    所以基本上您的 SwitchNavigator 将始终具有三个屏幕,将 initialRouteName 设置为 "Splash"

    此外,您还需要使用 createSwitchNavigator 而不是 SwitchNavigator

    【讨论】:

    • 嗨!感谢您的快速回答布拉德!通过将逻辑传递给splash,您的意思是在Splash 中运行firebase 函数?在初始路由中,您是否建议仅将 initialRouteName 设置为 Splash?
    • 没错!此外,您不需要在函数中创建 SwitchNavigator,它也可以直接分配给 App。
    • 您还可以使用活动指示器处理加载...我更新以显示 componentDidMount 的外观。
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    相关资源
    最近更新 更多