【发布时间】:2016-10-28 14:04:53
【问题描述】:
我有一个登录函数,它通过 Firebase 进行身份验证,然后应该调用从父级传递下来的登录函数作为道具。这里是:
class HeaderParentComponent extends Component {
toggleSignupLayoverAction(event){
this.props.toggleSignupLayover("true")
}
signIn(e){
e.preventDefault();
var email = $(".login-email").val();
var password = $(".login-user-password").val();
firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
var user = firebase.auth().currentUser;
this.props.logUserIn(user);
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
}
render() {
return (
<div className="header-inner">
<span onClick={this.props.toggleMenuOut} className="menuToggle"></span>
<a className="logo-a-tag" href="#">
<img height="25" src="../../images/logo.png" alt="my logo"/>
</a>
<form className="header-searchbox">
<input placeholder="Search Samples Here..." onChange={this.props.updateSearch} value={this.props.searchInputVal} type="text"></input>
</form>
<div className="header-acc"><a className={"login " + this.props.loggedInState} href="#">Login</a><a onClick={this.toggleSignupLayoverAction.bind(this)} className={"create " + this.props.loggedInState} href="#">Create Account</a>
<div className={"logged-in-header " + this.props.loggedInState}>Hello {this.props.LoggedInUsername}</div>
</div>
<div className="login-popup-par">
<form>
<input placeholder="Email Address" className="login-email" type="text"></input>
<input placeholder="Password" className="login-user-password" type="password"></input>
<button onClick={this.signIn.bind(this)}>Login</button>
</form>
</div>
</div>
)
}
}
目前从 parent 传下来的 props 有一个基本的 console.log 为“logged in”。它通过 this.props.logUserIn(user); 道具传递。问题是我试图在使用 Firebase 的身份验证成功后调用它,因此为什么我将它放在 .signInWithEmailAndPassword(email, password) 之后的 .then 函数中似乎我无法从 Auth 函数内部调用 this.props.logUserIn 但我不知道为什么。如果我将 props 函数移到 auth 函数之外,它会调用它没有问题。为什么我不能从那里访问道具?
【问题讨论】:
-
使用箭头函数
-
小心细化安德鲁
标签: reactjs firebase firebase-authentication