redux-form:通过 mapStateToProps 将 initialValues 作为第二个参数传递无效

redux-form: passing initialValues by mapStateToProps as 2nd argument is not working

我在寻找如何将初始值传递给我的表单:

我在 redux-form 文档中发现将 mapStateToProps 作为第二个传递。参考:https://redux-form.com/6.0.0-alpha.5/examples/initializefromstate/

所以我尝试了以下方法:

IngredientForm = reduxForm(
{ 
  form: 'ingredient',
  validate,
  enableReinitialize : true
},
(state) => {
    return {
        initialValues : state.ingredients.data)
     }
}
)(IngredientForm)

这里是 mapStateToProps 作为第二个参数。但是我发现 initialValues 没有传递给道具。

然后我搜索并找到了以下添加 mapStateToProps 的有效方法:

const mapStateToProps = (state) => {
    return {
        initialValues : state.ingredients.data
     }
}

 IngredientForm = connect( mapStateToProps )(
      reduxForm(
      {
        form: 'ingredient',
        validate,
      }
      )(IngredientForm)
 );

为什么第二个参数方式不起作用。

如果查看链接的示例,您会发现它还提供 mapStateToProps 作为 connect 的参数,而不是 reduxForm:

// You have to connect() to any reducers that you wish to connect to yourself
InitializeFromStateForm = connect(
  state => ({
    initialValues: state.account.data // pull initial values from account reducer
  }),
  { load: loadAccount }               // bind account loading action creator
)(InitializeFromStateForm)

Source 以上。