繁体   English   中英

导航在React Native App中不起作用

[英]Navigation not working in react native app

我正在开发具有Firebase身份验证的React Native应用程序。 但是,当我成功登录时,导航显示了一些问题,例如,未定义不是对象(正在评估“ this.props.navigation”)。 但用于导航的同一个圆锥体在其他页面上也可以正常工作。 我尝试了很多东西,但是没有用。伙计们请帮助我。

      import React from "react";
      import {
        Text,
        View,
        Image,
        TouchableOpacity,
        AsyncStorage,
        TextInput
      } from "react-native";
      import { styles } from "./Css";
      import { KeyboardAvoidingView } from "react-native";
      import { RkTextInput, RkButton } from "react-native-ui-kitten";
      import { Actions } from "react-native-router-flux";
      import { Icon } from "react-native-elements";

      import * as firebase from "firebase";
      export default class Login extends React.Component {
        constructor(props) {
          super(props);

          this.state = {
            email: "",
            password: "",
            userId: "",
            errorMessage: null,
          };
        }
        componentDidMount() {
          this._loadInitialState().done();
        }
        _loadInitialState = async () => {
          var value = await AsyncStorage.getItem("user");

          if (value !== null) {
            this.props.navigation.navigate("NewHome")
          }
        };

        handleLogin = (email, password) => {
          if(this.state.email.length<1){
            alert("Enter an Email");
            return;
          }

          if (this.state.email.includes("@")==false){
              alert("Use an email as Username");
              return;
            }

          if(this.state.password.length<6){
            alert("please enter correct password")
            return;
          }

          //alert(firebase.auth().currentUser.uid)
          firebase.auth().signInWithEmailAndPassword(email, password).then(function(user){
            if(user){
              this.props.navigation.navigate("NewHome"), //problem
              //--------------------------Async Test--------------------------
              AsyncStorage.setItem("user", firebase.auth().currentUser.uid)
              //--------------------------------------------------------------
            }else{

            }
          }


            )
            .catch(function(error) {
              var errorCode = error.code;
              var errorMessage = error.message;

              if (errorCode === "auth/wrong-password") {
                alert("Wrong password.");
                return;
              } else {
                alert(errorMessage);
                return;
              }
              console.log(error);
            });
        };

this不再指向您的React类。

一个解决方案可能是使用箭头功能:

firebase.auth().signInWithEmailAndPassword(email, password).then((user) => {...}

或者您必须先将其保存在函数中,然后才能在firebase回调中使用它。

let props = { ...this.props };

在回调中使用它进行导航:

props.navigation.navigate('NewHome');

暂无
暂无

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

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