简体   繁体   English

将道具传递给高阶组件

[英]Passing props to Higher-Order Component

I have a higher-order component FormBuilder like this: 我有一个高阶组件FormBuilder如下所示:

const FormBuilder = (WrappedComponent) => {
  return class HOC extends React.Component {
    clearForm() { // ... }

    render() {
      return (
        <Form onSubmit={//what do I say here?!}>
           <Form.Input placeholder='Name' name='name' />
           <WrappedComponent clearForm={this.clearForm} />
        <Form>
      );
    }
  }
}

And here is the WrappedComponent NewPizzaForm : 这是WrappedComponent NewPizzaForm

class WrappedComponent extends React.Component {
  onSubmit() { // sends a POST request to the backend, then this.props.clearForm() }

  render() {
     return (
       <Form.Button>Add Pizza</Form.Button>
     );
  }
}

const NewPizzaForm = FormBuilder(WrappedComponent);

export default NewPizzaForm;

So I want to send the onSubmit function as a prop from the WrappedComponent to the FormBuilder so that it is available for call when the form is submitted. 所以我想将onSubmit函数作为WrappedComponent的prop WrappedComponentFormBuilder以便在提交表单时可以调用它。 And the reason I decided to define the onSubmit function inside WrappedComponent is because I have another WrappedComponent (uses FormBuilder ) that has the onSubmit function but it sends a PATCH request rather than POST request. 我决定在WrappedComponent定义onSubmit函数的WrappedComponent是因为我有另一个具有onSubmit函数的WrappedComponent (使用FormBuilder )但是它发送了一个PATCH请求而不是POST请求。 How do I achieve this? 我该如何实现这一目标?

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. 作为参数,您可以添加prop“submit”以传递方法。

Ref: https://reactjs.org/docs/higher-order-components.html#dont-mutate-the-original-component-use-composition 参考: 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. 我想我们可能需要更多关于项目结构的信息,但是你可以在FormBuilder( funcA )中创建一个函数,传递给WrappedComponent,它将函数作为参数。 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. 然后当你单击WrappedComponent中的按钮时,它会将自己的onSubmit函数发送回funcA ,它可以在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. 然后可以在您的其他WrappedComponent(使用POST请求)上使用它,因为您将从两者中发送onSubmit函数以在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. 我不确定这是否可行,但也许你可以将表单提交的结果保存到HOC的状态,然后通过props将这些信息传递给WrappedComponent Then using getDerivedStateFromProps inside of WrappedComponent , you can pass the submitted form information into the component's submit function. 然后在WrappedComponent使用getDerivedStateFromProps ,您可以将提交的表单信息传递给组件的提交函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM