我正在尝试根据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
};
慕森卡
相关分类