更新表单时 initialValues 没有得到更新
initialValues does not get update when updating the form
我有一个名为 Client
的表单,它有一个表单可以同时处理 add
和 edit
。添加工作但是在编辑表单时(表单
如果它是和编辑表单,则填充有 initialValues),initialValues 不会更新。我的意思是,如果我去客户 A 表格并更新
从 'abc' 到 'xyz' 调用 client_name
的字段然后 client_name 将在服务器中保存为 'xyz' 但 initialValues 没有得到更新
因此,如果我再次转到相同的表单而不刷新页面并保存表单而不更改任何内容,那么 client_name
将与上一个一起保存
值一。 'abc' 因为更新字段的时候并没有更新initialValues。
这是我的代码
import React, { Component } from 'react';
import { reduxForm, initialize } from 'redux-form';
const mapDispatchToProps = (dispatch, props) => ({
addClient: clientData => dispatch(addClient(clientData)),
editClient: clientData => dispatch(editClient(clientData)),
loadClient: () => dispatch(loadClient(props.match.params.company)),
resetClient: () => dispatch(resetClient()),
});
const mapStateToProps = createStructuredSelector({
initialValues: selectClient(),
});
class Client extends Component<propsCheck> {
state = {
client: initialState,
isLoading: false,
};
componentDidMount() {
if (this.props.match.params.company) {
this.props.loadClient();
}
}
componentWillReceiveProps(nextProps) {
if (this.props.match.params.company) {
this.props.loadClient();
} else {
this.props.resetClient();
}
}
handleChange = ({ target: { name, value } }) => {
this.setState({ client: { ...this.state.client, [name]: value } });
};
handleSubmit = event => {
event.preventDefault();
const { client } = this.state;
const { initialValues, addClient: add, editClient: edit } = this.props;
if (isEmpty(initialValues)) {
add(client);
} else {
const updatedClient = updatedValue(initialValues, client, 'id');
edit(updatedClient);
}
this.setState({ isLoading: true });
};
render() {
const { invalid, submitting, initialValues } = this.props;
return (
<ClientForm
loading={this.state.isLoading}
handleChange={this.handleChange}
handleSubmit={this.handleSubmit}
disabled={invalid || submitting}
type={initialValues && initialValues.id ? 'Edit' : 'Add'}
reset={this.props.reset}
history={this.props.history}
/>
);
}
}
const withReduxForm = reduxForm({
form: 'clientForm',
fields: requiredFields,
validate,
enableReinitialize: true,
})(Client);
export default connect(
mapStateToProps,
mapDispatchToProps,
)(withReduxForm);
您的初始值是从 redux 状态(mapStateToProps 中的 selectClient)填充的,对吗?所以,当你 update/edit client_name 时,你是在更改 redux 中的数据吗??
我有一个名为 Client
的表单,它有一个表单可以同时处理 add
和 edit
。添加工作但是在编辑表单时(表单
如果它是和编辑表单,则填充有 initialValues),initialValues 不会更新。我的意思是,如果我去客户 A 表格并更新
从 'abc' 到 'xyz' 调用 client_name
的字段然后 client_name 将在服务器中保存为 'xyz' 但 initialValues 没有得到更新
因此,如果我再次转到相同的表单而不刷新页面并保存表单而不更改任何内容,那么 client_name
将与上一个一起保存
值一。 'abc' 因为更新字段的时候并没有更新initialValues。
这是我的代码
import React, { Component } from 'react';
import { reduxForm, initialize } from 'redux-form';
const mapDispatchToProps = (dispatch, props) => ({
addClient: clientData => dispatch(addClient(clientData)),
editClient: clientData => dispatch(editClient(clientData)),
loadClient: () => dispatch(loadClient(props.match.params.company)),
resetClient: () => dispatch(resetClient()),
});
const mapStateToProps = createStructuredSelector({
initialValues: selectClient(),
});
class Client extends Component<propsCheck> {
state = {
client: initialState,
isLoading: false,
};
componentDidMount() {
if (this.props.match.params.company) {
this.props.loadClient();
}
}
componentWillReceiveProps(nextProps) {
if (this.props.match.params.company) {
this.props.loadClient();
} else {
this.props.resetClient();
}
}
handleChange = ({ target: { name, value } }) => {
this.setState({ client: { ...this.state.client, [name]: value } });
};
handleSubmit = event => {
event.preventDefault();
const { client } = this.state;
const { initialValues, addClient: add, editClient: edit } = this.props;
if (isEmpty(initialValues)) {
add(client);
} else {
const updatedClient = updatedValue(initialValues, client, 'id');
edit(updatedClient);
}
this.setState({ isLoading: true });
};
render() {
const { invalid, submitting, initialValues } = this.props;
return (
<ClientForm
loading={this.state.isLoading}
handleChange={this.handleChange}
handleSubmit={this.handleSubmit}
disabled={invalid || submitting}
type={initialValues && initialValues.id ? 'Edit' : 'Add'}
reset={this.props.reset}
history={this.props.history}
/>
);
}
}
const withReduxForm = reduxForm({
form: 'clientForm',
fields: requiredFields,
validate,
enableReinitialize: true,
})(Client);
export default connect(
mapStateToProps,
mapDispatchToProps,
)(withReduxForm);
您的初始值是从 redux 状态(mapStateToProps 中的 selectClient)填充的,对吗?所以,当你 update/edit client_name 时,你是在更改 redux 中的数据吗??