gpt4 book ai didi

javascript - Redux 表单卸载组件 onSubmit

转载 作者:行者123 更新时间:2023-12-03 02:25:18 24 4
gpt4 key购买 nike

所以我现在有一个表格,我想更新名字和姓氏。如果出现错误,我想捕获并显示它。问题是我收到错误,如果出现错误,ReduxForm 只能更新已安装或正在安装的组件。原因是 redux-form 正在卸载并重新安装我的组件。有没有办法避免重新安装组件?

  class EditUser extends Component {
constructor(props) {
super(props);
this.onSubmit = this.onSubmit.bind(this);
this.state = {};
}

onSubmit(values) {
const { fname, lname } = values;
return this.props.editUser(fname, lname).catch(err => this.setState({
error: err
});
}

render() {
const { handleSubmit, submitting } = this.props;
return (
<Container>
<Row>
<Col sm={{ size: 8, offset: 2 }}>
<div>
<form onSubmit={handleSubmit(this.onSubmit)}>
<FormGroup>
<Field
label="First Name"
type="input"
name="fname"
id="fname"
component={FieldInput}
placeholder="First Name"
validate={required}
/>
</FormGroup>
<FormGroup>
<Field
label="Last Name"
type="input"
name="lname"
id="lname"
component={FieldInput}
placeholder="Last Name"
validate={required}
/>
</FormGroup>
<Button color="success" className="float-right" disabled={submitting}>Submit</Button>
</form>
</div>
</Col>
</Row>
</Container>
);
}
}

function mapStateToProps(state) {
const { first_name, last_name } = state.user;
return {
user: state.user,
initialValues: {
fname: first_name,
lname: last_name
}
};
}

export default connect(mapStateToProps, { editUser })(reduxForm({
form: 'EditUserForm',
})(EditUser));

最佳答案

我认为你应该使用onSubmitFailonSubmitSuccess处理 onSubmit 回调

关于javascript - Redux 表单卸载组件 onSubmit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48978743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com