【问题标题】:React navigation doesnt navigate反应导航不导航
【发布时间】:2018-12-22 13:40:54
【问题描述】:

我正在开发一个应用程序,并在我的AuthenticationLoadingScreen 中使用来自 Firebase 的侦听器 onAuthStateChanged,因此我可以根据此状态重定向用户,但是,它只是打开一个白页而不打开主屏幕。

编辑:有人说app.js 可能是问题,所以我在这里添加了它

import HomeScreen from '../screens/HomeScreen'
import AuthScreen from '../screens/AuthScreen'

// Implementation of HomeScreen, OtherScreen, SignInScreen, AuthLoadingScreen
// goes here.

const AppStack = createStackNavigator({ Home: HomeScreen});
const AuthStack = createStackNavigator({ Home: HomeScreen });

export default createAppContainer(createSwitchNavigator(
  {
    AuthLoading: AuthScreen,
    App: AppStack,
    Auth: AuthStack
  },
  {
    initialRouteName: 'AuthLoading',
  }
));

export default class AuthScreen extends React.Component {

  constructor(props){
      super(props);
  }

  componentDidMount = ()=>{
    firebase.auth().onAuthStateChanged(user => {
      this.props.navigation.navigate(user ? 'App' : 'Auth');

    });
  }

  render() {
    return (
      <ImageBackground
        source={require('../assets/images/AuthScreenBackground.png')}
        style={styles.ImageBackground}/>
    );
  }
}

这里是App.js 代码:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import AppNavigation from './navigation/AppNavigation.js'


import * as firebase from 'firebase'

var config = {

  };
firebase.initializeApp(config);


export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <AppNavigation/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

【问题讨论】:

    标签: javascript reactjs firebase react-native firebase-authentication


    【解决方案1】:

    试试这个代码:

    componentWillMount() {
        this.observeLogin = firebase.auth().onAuthStateChanged(user => {
            if (user) {
                this.props.navigation.navigate('App');
            } else {
                this.props.navigation.navigate('Auth');
            }
    
        });
    }
    
    componentWillUnmount() {
        // Don't forget to unsubscribe when the component unmounts
        this.observeLogin ();
    }
    

    【讨论】:

    • 你能把你的App组件代码放到你的问题上吗?因为如果它已经移动到不同的屏幕,这意味着导航工作,我认为来自组件的错误。
    猜你喜欢
    • 2020-01-28
    • 1970-01-01
    • 2020-08-01
    • 2018-01-02
    • 1970-01-01
    • 2022-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多