我正在尝试根据prop( isSignUp) 是true还是false在我的状态下向 SIGN UP 或 LOGIN 用户发送 POST 请求。我想要发生的事情:如果是真的,我希望一个 API 端点被击中,如果isSignUp不是,另一个被击中。各个端点都可以工作,但是当我将所有东西连接起来时,我只能让人们登录,而一旦他们注册就无法登录。状态:state = { controls: { email: { elementType: "input", elementConfig: { type: "email", placeholder: "Email Address" }, value: "", validation: { required: true, // must not be empty isEmail: true }, valid: false, touched: false }, isSignUp: true }};处理程序:handleSubmit = event => { this.props.onAuth( this.state.controls.email.value, this.state.controls.password.value, this.state.controls.isSignUp ); event.preventDefault(); }; switchAuthModeHandler = () => { this.setState(prevState => { console.log(prevState); return { isSignUp: !prevState.isSignUp }; }); };切换按钮文本和触发 authModeHandler 的逻辑 return ( <div className={classes.Auth}> <form onSubmit={this.handleSubmit}> {form} <Button btnType="Success">SUBMIT</Button> </form> <Button clicked={this.switchAuthModeHandler} btnType="Danger"> SWITCH TO {this.state.isSignUp ? "SIGN IN" : "SIGN UP"} {console.log(this.state.isSignUp)} </Button> </div> );const mapDispatchToProps = dispatch => { return { onAuth: (email, password, isSignUp) => dispatch(actions.auth(email, password, isSignUp)) };};触发 POST 请求以登录或注册用户的操作export const auth = (email, password, isSignUp) => { return dispatch => { dispatch(authStart()); const authData = { email: email, password: password, returnSecureToken: true };
1 回答
慕森卡
TA贡献1806条经验 获得超8个赞
在状态下,您isSignup 不将其置于控制之下,因此您应该通过以下方式访问它this.state.isSginUp
handleSubmit = event => {
this.props.onAuth(
this.state.controls.email.value,
this.state.controls.password.value,
this.state.isSignUp
);
event.preventDefault();
};
PS 我建议使用 Redux 开发工具。它可以挽救生命。快乐编码!
添加回答
举报
0/150
提交
取消