Redux的initialValues未正确更新

时间:2018-03-08 18:25:40

标签: redux-form

我有一个看起来像这样的组件,

const errorAnalysisName = { name: undefined }

const ReduxForm = (props: ReduxFormProps) => {
  const { handleSubmit, children } = props
  return <Form onSubmit={handleSubmit}>{children}</Form>
}

const connectedForm = connect(state => {
  errorAnalysisName.name = 'insideRedux'
  return {
    errorAnalysisName: 'insideRedux'
  }
}, null)(ReduxForm)

export default reduxForm({
  form: 'CreateWizard',
  enableReinitialize: true,
  validate,
  destroyOnUnmount: false,
  initialValues: {
    baseScheduleChoice: 'load',
    newScheduleChoice: 'load',
    currentIndex: 0,
    name: errorAnalysisName.name
  }
})(ReduxForm)

字段name未更新为insideRedux,有人看到我在这里缺少的内容吗?

我想从我的状态传递初始值,但尝试这样做不会更新reduxForm中的initialValues对象。

1 个答案:

答案 0 :(得分:1)

如果你想使用useReinitialize你将不会在渲染redux表单时设置initialValues prop,那么如果prop更改了表单将重新初始化。

为简洁起见,我稍微修改了您的示例:

const CreateWizard = reduxForm({
  form: 'CreateWizard',
  enableReinitialize: true,
  destroyOnUnmount: false
})(({ handleSubmit, children }) => (
  <form onSubmit={handleSubmit}>{children}</form>
));

const Container = connect(state => ({
  errorAnalysisName: state.errorAnalysisName
})(props => (
  <CreateWizard initialValues={{
    baseScheduleChoice: 'load',
    newScheduleChoice: 'load',
    currentIndex: 0,
    name: props.errorAnalysisName
  }} />
));

export default Container;