multiple Redux-form with formValueSelector

北战南征 提交于 2020-01-06 14:20:13

问题


I'm trying to create multiple forms that have dependable dropdowns. Based on selection on dropdown1, some fields are shown and another dropdown is populated.

To achieve the multiple forms, I have to pass a unique form key such as:

panels.map(panel =>
  <PanelForm key={panel.uuid} form={`PanelForm_${panel.uuid}`} />
)

However, to connect to state for changes, I have to use the redux formValueSelector which requires to set it to the form name passed which is dynamic and I don't know how to pass it here...

const selector = formValueSelector('PanelForm_XXXX')
                                    ^^^^^^^^^^^^^^
const FormConnectDecorator = connect((state) => {
  const category = selector(state, 'category')
  return {
    category,
  }
})(Form)

const FormDecoratedComponent = reduxForm()(FormConnectDecorator)

I need to connect the form to redux state to read category value but can't seem to pass the correct dynamic form name value to it.


回答1:


After a bit more browsing, as formValueSelector returns a function, the solution is to use it in mapStateToProps. Credits to rizedr on https://github.com/erikras/redux-form/issues/1987

const mapStateToProps = (state, initialProps) => {
  return {
    category: (formValueSelector(initialProps.form))(state, 'category'),
  };
};
const FormConnectDecorator = connect(mapStateToProps)(Form)

const FormDecoratedComponent = reduxForm()(FormConnectDecorator)

export default FormDecoratedComponent

@Mods - Feel free to delete this question if you will. Answer already exists in redux-form github closed issues, link provided above.



来源:https://stackoverflow.com/questions/56635325/multiple-redux-form-with-formvalueselector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!