2

我有一个有趣的问题。

当我按下注册按钮时,在注册组件中调用下面的函数。

//SignUp.js
signUpHandler = () => {
        if (this.state.email == '' || this.state.password == '') {
            alert("Email or Password is empty.")
        }
        else {
            firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
                .then(() => this.props.navigation.navigate('ExtendedRegisteration'))
                .catch(error => { alert(error.message) });
        }
    }

现在这不会导航到“ExtendedRegisteration”,但是当我执行以下操作时,它导航得很好。

//SignUp.js
signUpHandler = () => {
        if (this.state.email == '' || this.state.password == '') {
            alert("Email or Password is empty.")
        }
        else {
                this.props.navigation.navigate('ExtendedRegisteration')
        }
    }

这是 Firebase 的问题吗?使用 firebase 注册后,我需要导航到“ExtendedRegisteration”。

signUpHandler 中 this.props 的输出:

Object {
  "navigation": Object {
    "addListener": [Function addListener],
    "canGoBack": [Function canGoBack],
    "dangerouslyGetParent": [Function dangerouslyGetParent],
    "dangerouslyGetState": [Function anonymous],
    "dispatch": [Function dispatch],
    "goBack": [Function anonymous],
    "isFocused": [Function isFocused],
    "jumpTo": [Function anonymous],
    "navigate": [Function anonymous],
    "pop": [Function anonymous],
    "popToTop": [Function anonymous],
    "push": [Function anonymous],
    "removeListener": [Function removeListener],
    "replace": [Function anonymous],
    "reset": [Function anonymous],
    "setOptions": [Function setOptions],
    "setParams": [Function anonymous],
  },
  "route": Object {
    "key": "Sign Up-q-zIoH0VCp",
    "name": "Sign Up",
    "params": undefined,
  },
}
4

4 回答 4

1

这不是 Firebase 的问题。您遇到的问题是从承诺返回后执行的箭头函数中访问导航引用。的范围与this该箭头函数内的范围不同。

为了解决这种情况,React Navigation提供了一个解决方案,您可以在其中定义一个函数来直接调用导航。

import { createNavigationContainerRef } from '@react-navigation/native';

export const navigationRef = createNavigationContainerRef()

export function navigate(name, params) {
  if (navigationRef.isReady()) {
    navigationRef.navigate(name, params);
  }
}  

然后你可以像这样简单地导入navigate和使用它。

navigate('ExtendedRegisteration');

这在任何地方都不常用的原因是,当您有复杂的嵌套路线时,它可能会出现问题,而且它也不能让您访问其他导航方法,但对于像您这样的情况,它可以正常工作。

于 2021-08-02T09:40:00.400 回答
0

确保在导航之前卸载 FirebaseRecaptcha.FirebaseRecaptchaVerifierModal

于 2021-08-01T10:36:54.180 回答
0

从调用注册函数的任何位置将 this 作为参数传递,并使用该参数代替 this。

于 2020-03-21T20:31:04.540 回答
0

'this' 表示上下文,这是反应中最有趣的事情。尝试使用 console.log(this.props) 检查 firebase 中的 props 是否与另一个示例相同。由于 this 是从 Promise 内部调用的,因此它可能会更改“this”上下文。它发生的次数比预期的要多。

你可以尝试创建一个函数

constructor(props){
    super(props);
    this._onUserCreation = this._onUserCreation.bind(this);
}

_onUserCreation=()=>{this.props.navigation.navigate('ExtendedRegisteration')};

//SignUp.js
signUpHandler = () => {
        if (this.state.email == '' || this.state.password == '') {
            alert("Email or Password is empty.")
        }
        else {
            firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
                .then(() => this._onUserCreation())
                .catch(error => { alert(error.message) });
        }
    }

于 2020-03-21T19:54:19.153 回答