【发布时间】:2019-04-22 11:47:38
【问题描述】:
我只想在我从减速器更新收到的道具之后在我的表单上运行 onSubmit。
const mapStateToProps = state => {
return {
authError: state.auth.authError // I want this to update before the action.
};
};
如果我在 onSutmit 处理程序中使用 console.log authError,我会收到旧的 authError。第二次运行时,我收到更新的 authError。
handleSubmit = e => {
e.preventDefault();
console.log("error exists?", this.props.authError);
this.props.signIn(this.state); //dispatches a login action
this.handleHideLogin(); // hides the form after action is dispatched
};
我只想在操作被调度并且错误为空后隐藏表单。 (认证成功则自动返回null)
我尝试使用 setTimeout() 并且它在技术上有效,但我想知道是否有更“正确”的方法来做到这一点。
handleSubmit = e => {
e.preventDefault();
this.props.signIn(this.state);
setTimeout(() => {
if (this.props.authError) {
console.log("returns error =>", this.props.authError);
} else {
console.log("no error =>", this.props.authError);
this.handleHideLogin();
}
}, 500);
};
我的部分组件供参考
<form onSubmit={!this.props.authError ? this.handleSubmit : null}>
//the above onSubmit is different if I use the setTimeout method.
<div className="modal-login-body">
<div className="modal-login-input">
<input
type="email/text"
name="email"
autoComplete="off"
onChange={this.handleChange}
required
/>
<label htmlFor="email" className="label-name">
<span className="content-name">EMAIL</span>
</label>
</div>
<div className="modal-login-input">
<input
type="password"
name="password"
autoComplete="off"
onChange={this.handleChange}
required
/>
<label htmlFor="password" className="label-name">
<span className="content-name">PASSWORD</span>
</label>
</div>
</div>
<div className="my-modal-footer">
<p className="login-failed-msg">
{this.props.authError ? this.props.authError : null}
</p>
<button type="submit" className="complete-login-button">
LOGIN
</button>
<a href="CHANGE" className="forgot-password">
<u>Forgot your password?</u>
</a>
</div>
</form>
【问题讨论】:
标签: javascript reactjs firebase redux firebase-authentication