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

Add props component into props parameter from reduxform callback

我想知道这种情况是否可能以某种方式实现:

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);

所以基本上,我需要根据 props1props2onSubmitFailonSubmitSuccess 回调中的值做一些条件性的事情。

根据 documentation,我没有看到任何这样做的细节。

谢谢

我找到了解决我的问题的有效方法。可能对遇到同样问题的人有用。

这里是:

你的组件必须是这样的形状:

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

根据 reduxform documentation,您可以从 reduxform 作为 props handleSubmit 传递。因此,您可以根据需要为 handleSubmit 函数提供参数(在我的例子中:props1props2

然后myCustomSubmitFonction需要return它:

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

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

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);