【发布时间】:2017-11-12 04:32:14
【问题描述】:
我正在尝试使用 redux 表单注册用户。所有同步验证都在工作,但是当用户提交表单并且电子邮件已经注册时,问题就出现了。返回一个错误,我可以按如下方式访问该错误
export function registerUser(details, callback) {
return(dispatch) =>{
dispatch(startRegisteing());
axios.post(URL, {
"email": details['email'],
"name": details['username'],
"password": details['password']
}).then((data) =>{
callback();
dispatch(RegistrationSuccessfull(data))
}).catch((error) => {
console.log('error', error.response.data.message);
//throw new SubmissionError(error.response.data.message);
dispatch(RegistrationFailed(error.response.data.message))
})
}
}
RegistrationFailed 操作
export function RegistrationFailed(error) {
return{
type: types.REGISTRATION_ERROR,
payload:error
}
}
它的 reducer 只是更新状态
switch (action.type){
case types.REGISTRATION_SUCCESSFUL:
console.log("nanana too ", action);
return{
...state, isRegisteringUser:false,
data:action.data
};
case types.REGISTRATION_ERROR:
console.log("current state", state);
return{
...state, error: action.payload
}
}
我的问题是我怎样才能在表单中将此错误呈现给用户
<form onSubmit={handleSubmit(this.onFormSubmit.bind(this))}>
<Field
name="username"
lable="Username"
type="text"
component={ this.renderField }
/>
<Field
name="email"
type="email"
lable="Email"
component={ this.renderField }
/>
{// I want to present the error here }
{error && <strong>{error}</strong>}
<Field
name="password"
type="password"
lable="Password"
component={ this.renderField }
/>
<Field
name="confirm_password"
type="password"
lable="Confirm Password"
component={ this.renderField }
/>
<button type="submit" className="btn btn-primary" disabled={submitting}>Register</button>
<Link className="btn btn-danger" to="/login">Cancel</Link>
</form>
我已经尝试过 throw new SubmissionError(error.response.data.message);,正如 react-form 文档所说,但它引发了 Unhandled rejection SubmissionError: Submit Validation Failed 的错误
注意: 我是 react-redux 的初学者。任何帮助将不胜感激
【问题讨论】:
标签: javascript reactjs react-redux react-redux-form