导航在React Native App中不起作用

时间:2019-02-06 16:24:26

标签: reactjs firebase react-native navigation

我正在开发具有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);
            });
        };

1 个答案:

答案 0 :(得分:0)

void foo(const arg& input) { // read from input } 不再指向您的React类。

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

this

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

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

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

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