Passing props to Higher-Order Component

妖精的绣舞 提交于 2019-11-30 23:22:12

You can act as following:

function logProps(InputComponent) {
  InputComponent.prototype.componentWillReceiveProps = function(nextProps) {
    console.log('Current props: ', this.props);
    console.log('Next props: ', nextProps);
  };
  // The fact that we're returning the original input is a hint that it has
  // been mutated.
  return InputComponent;
}

// EnhancedComponent will log whenever props are received
const EnhancedComponent = logProps(InputComponent);

As parameter you can add the prop "submit" to pass in the method.

Ref: https://reactjs.org/docs/higher-order-components.html#dont-mutate-the-original-component-use-composition

I think we might need a little more information about the structure of your project, but you could create a function within FormBuilder (funcA) that you pass down to the WrappedComponent that takes a function as an argument. Then when you click the button within WrappedComponent, it would send its own onSubmit function back up to funcA where it can be used within FormBuilder.

This can then be used on your other WrappedComponent (with the POST request) as you would just be sending the onSubmit function from both to be called within FormBuilder.

Hope this helps.

I'm not at all sure if this would work, but maybe you could save the result of the form submission into the HOC's state, and then pass that information down to WrappedComponent via props. Then using getDerivedStateFromProps inside of WrappedComponent, you can pass the submitted form information into the component's submit function.

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