React导航无法与React本机应用程序中的Firebase一起使用
我有一个有趣的问题 当我按下注册按钮时,下面的函数在注册组件中被调用React导航无法与React本机应用程序中的Firebase一起使用,firebase,react-native,react-navigation,react-navigation-v5,Firebase,React Native,React Navigation,React Navigation V5,我有一个有趣的问题 当我按下注册按钮时,下面的函数在注册组件中被调用 //SignUp.js signUpHandler = () => { if (this.state.email == '' || this.state.password == '') { alert("Email or Password is empty.") } else { firebase.auth().createU
//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) });
}
}
现在,它不会导航到“ExtendedRegistration”,但当我执行下面的操作时,它导航得很好
//SignUp.js
signUpHandler = () => {
if (this.state.email == '' || this.state.password == '') {
alert("Email or Password is empty.")
}
else {
this.props.navigation.navigate('ExtendedRegisteration')
}
}
这是Firebase的问题吗?
使用firebase注册后,我需要导航到“ExtendedRegistration”
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,
},
}
“this”指的是上下文,它是react中最有趣的东西。 尝试console.log(this.props)检查firebase中的道具是否与其他示例相同。 由于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) });
}
}
“this”指的是上下文,它是react中最有趣的东西。 尝试console.log(this.props)检查firebase中的道具是否与其他示例相同。 由于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) });
}
}
无论从何处调用注册函数,都将此作为参数传递,并使用该参数代替此参数。无论从何处调用注册函数,都将此作为参数传递,并使用该参数代替此参数。此操作运气不佳。我还是有同样的问题。为什么我们要在这里创建一个单独的导航函数呢?创建单独的函数是为了绑定上下文。我的问题就是这样解决的。console.log(this.props)显示了什么?我已经在上面的问题中添加了输出。这没有什么好运气。我还是有同样的问题。为什么我们要在这里创建一个单独的导航函数呢?创建单独的函数是为了绑定上下文。我的问题就是这样解决的。console.log(this.props)显示了什么?我已经在上面的问题中添加了输出。