从reduxform回调将props组件添加到props参数中

时间:2019-03-29 15:53:04

标签: reactjs redux redux-form

我想知道这种情况是否可能:

const MyComponent = ({props1, props2}) => (
  <div >
    // SOME STUFF using props1 and props2
  </div>
);


const ReduxFormWithMyComponent = reduxForm({
  form: 'test',
  onSubmitFail: (errors, dispatch, submitError, props) => {
    // here I would like to have access to props1 and props2 like:
    // const { props1, props2 } = props;
  },
  onSubmitSuccess: (result, dispatch, props) => {
    // same here:
    // const { props1, props2 } = props;
  },
})(MyComponent);

因此,基本上,我需要根据props1props2回调中onSubmitFailonSubmitSuccess的值来做一些条件式的东西。

根据documentation,我没有看到任何详细信息。

谢谢

1 个答案:

答案 0 :(得分:0)

我找到了有效的解决方案。对于遇到相同问题的人可能很有用。

在这里:

您的组件必须像这样的形状:

const MyComponent = ({handleSubmit, props1, props2}) => (
  <div >
  <form
    onSubmit={handleSubmit(data => myCustomSubmitFonction(data, {props1, props2}))}
  >
    // SOME STUFF using props1 and props2
  </form>
  </div>
);

根据reduxform documentation,您可以作为道具handleSubmit从reduxform传递。结果,您可以根据需要为handleSubmit函数提供参数(在我的情况下为props1props2

然后myCustomSubmitFonction需要返回它:

myCustomSubmitFonction(reduxFormData, myCustomData) {
    // reduxFormData useless in my case
    return myCustomData;
}

现在,我可以通过props1(第一个参数)访问props2result

const ReduxFormWithMyComponent = reduxForm({
  form: 'test',
  onSubmitFail: (errors, dispatch, submitError, props) => {
    const { props1, props2 } = result;
    // Yay !!
  },
  onSubmitSuccess: (result, dispatch, props) => {
    const { props1, props2 } = result;
    // Yay !!
  },
})(MyComponent);