Redux表单:首次提交后未更新的字段

时间:2017-05-15 16:24:29

标签: redux redux-form redux-saga

我正在使用带有mongodb的redux-form,由于以下错误,我无法进行第二次提交:

{"message":"No matching document found for id \"590b02068012fb3f83e5da9d\"","name":"VersionError"}

这是由于文档的版本未在表单中更新。

第一次提交有效,数据提升;我从REST端点获取一个新对象,然后将其设置为状态。无论如何,表单提交的版本值不会更新,这会导致错误。我是否需要在onSubmitSuccess回调上手动触发此更新?

这是我的表格:

RegionEdit = reduxForm({
   form: 'RegionEdit', // a unique identifier for this form
})(RegionEdit);

const mapStateToProps = createStructuredSelector({
   initialValues: makeSelectRegion(),
});

function mapDispatchToProps(dispatch) {
   return {
      dispatch,
   };
}

export default connect(mapStateToProps, mapDispatchToProps)(RegionEdit);

这是我提交表单的方式:

submit(values) {
    return new Promise((resolve, reject) => {
      this.props.dispatch(saveRegion({ values, resolve, reject }));
    });
  }

执行以下传奇:

export function* saveRegion(data) {
  const payload = data.payload.values.toJS();
  const reject = data.payload.reject;
  const resolve = data.payload.resolve;
  const requestURL = `${ENDPOINT_URL}/${payload._id}`;
  try {
    const region = yield call(request, requestURL, {
      headers: {
        Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(payload),
      method: 'PUT' });
    yield put(regionSaved(region));
    resolve(region);
  } catch (err) {
    yield put(regionSavingError(err));
    reject(err);
  }
}

regionSaved触发SAVE_REGION_SUCCESS操作,导致此代码在reducer中执行:

 case SAVE_REGION_SUCCESS:
      return state
        .set('region', fromJS(action.region));

我附上一些截图  state field version is updated from 5 to 6 after the first successful submission 1

the payload of the second call has still old/initial version number (5) 2

1 个答案:

答案 0 :(得分:0)

我发现了问题,问题出在reduxForm实例化中,我只需要将属性enableReinitialize设置为true

RegionEdit = reduxForm({
   enableReinitialize: true,
   form: 'RegionEdit',  // a unique identifier for this form
})(RegionEdit);