ReduxForm: passing initialValues from mapStatetoProps

Viewed 2856

I'm trying to get the initialValues of the form setup as follows but it doesn't work:

const mapStateToProps = (state, props) => ({
  initialValues: state.charts[props.resourceId],
});

const mapDispatchToProps = ...

export default reduxForm({ 
  form: 'ChartForm',
})(connect(mapStateToProps, mapDispatchToProps)(ChartForm));

Of course if I do something like this will work:

export default reduxForm({ 
      form: 'ChartForm',
      initialValues: {
          title: "Some Title",
          ...
      }
})(connect(mapStateToProps, mapDispatchToProps)(ChartForm));

That is not what I need though. I need to get the initial values from a resource in my store that has id == this.props.resourceId

Could anybody point out what is wrong with the first solution?

1 Answers

You need to pass the initialValues props into the form. To do that wrap the form with the connect function. See Initialize From State in the documentation.

export default connect(mapStateToProps, mapDispatchToProps)(reduxForm({ 
  form: 'ChartForm',
})(ChartForm));
Related