简体   繁体   English

在 React 高阶组件中传播 props

[英]Spreading props in React Higher Order Components

I am trying to go very in-depth to understand the purpose of spreading props in React HOC我正在尝试 go 非常深入地了解在 React HOC 中传播道具的目的

So taking the below example;所以以下面的例子为例;

const EnhanceComponent = BaseComponent => {
    return class EnhancedComponent extends Component {
        state = {
            name: 'You have been enhanced'
        }
        render() {
           return ( 
           <BaseComponent {...this.props} {...this.state} />   
        )
       }
    }
};

export default EnhanceComponent;

Now let's say the usage of BaseComponent is as below;现在让我们说 BaseComponent 的用法如下;

<BaseComponent className='wrapper-container' onClick={this.handleClick} />

I assume if had not spread the props in the HOC, we would have been unable to access "this.props.className" OR "this.props.onClick" in BaseComponent.我假设如果没有在 HOC 中传播道具,我们将无法访问 BaseComponent 中的“this.props.className”或“this.props.onClick”。 Would that be correct understanding?那会是正确的理解吗?

class BaseComponent extends React.Component {
      render() {
         const { className, onClick} = this.props;

         ...
      }
   }

Now to use the HOC itself, we would say;现在要使用 HOC 本身,我们会说;

const EnhancedMyComponent = EnhanceComponent(MyComponent);

And render it as并将其渲染为

<EnhancedMyComponent className='wrapper-container' onClick={this.handleClick} />

Now, below are my 2 specific questions;现在,以下是我的 2 个具体问题;

  1. What do we finally render ie BaseComponent or EnhancedMyComponent OR using HOC allows us to use either flavor eg in some case, if we do not want the enhanced functionality, we just use the base component?我们最终渲染什么,即 BaseComponent 或 EnhancedMyComponent 或者使用 HOC 允许我们使用任何一种风格,例如在某些情况下,如果我们不想要增强的功能,我们只使用基本组件?

OR或者

<EnhancedMyComponent className='wrapper-container' onClick={this.handleClick} />
  1. Would the props access issue ie if we do not spread the props be applicable in both the above cases of consumption ie <BaseComponent /> AND <EnhancedMyComponent /> ?道具访问问题,即如果我们不传播道具,是否适用于上述两种消费情况,即<BaseComponent /><EnhancedMyComponent />

1) What do we finally render ie BaseComponent or EnhancedMyComponent OR using HOC allows us to use either flavor eg in some case, if we do not want the enhanced functionality, we just use the base component? 1)我们最终渲染什么,即 BaseComponent 或 EnhancedMyComponent 或者使用 HOC 允许我们使用任何一种风格,例如在某些情况下,如果我们不想要增强的功能,我们只使用基本组件?

/ Using HOC allows us to use either flavor. / 使用 HOC 允许我们使用任何一种风格。 It totally depends where we are wrapping the Component in HOC ie while exporting or while using it at someplace.这完全取决于我们在 HOC 中包装组件的位置,即在导出或在某个地方使用它时。

Now, In the below case one has the option to use it with or without HOC现在,在以下情况下,可以选择在有或没有 HOC 的情况下使用它

// BaseComponent.js 
        class BaseComponent extends React.Component {
                  render() {
                     const { className, onClick} = this.props;
            
                     ...
                  }
               }
    export default BaseComponent;
    
    // SomeComponent.js
    import BaseComponent from './BaseComponent';

    const MyComponent = EnhanceComponent(BaseComponent);
    class SomeComponent extends React.Component {
                  render() {
                     return ( 
                     ...
                     <MyComponent className={...} onClick={...} someExtraPropForHOC={...}/>
                     <BaseComponent  className={...} onClick={...} />
                     ...
                  )
               }

To not allow anyone to directly use the Component, wrap it in HOC and export为了不允许任何人直接使用组件,将其包装在 HOC 中并导出

// BaseComponent.js 
    class BaseComponent extends React.Component {
              render() {
                 const { className, onClick} = this.props;
        
                 ...
              }
           }
export default EnhanceComponent(BaseComponent);

// SomeComponent.js
import BaseComponent from './BaseComponent';
class SomeComponent extends React.Component {
              render() {
                 return ( 
                 ...
                 <BaseComponent className={...} onClick={...}/>
                 ...
              )
           }

2) Would the props access issue ie if we do not spread the props be applicable in both the above cases of consumption ie AND? 2)如果我们不传播道具,道具访问问题是否适用于上述两种消费情况,即AND?

/ Spread the props is needed as HOC does not know what props would be needed for the dynamically wrapped component. / 需要传播道具,因为 HOC 不知道动态包装的组件需要哪些道具。 So pass all the props which are coming is the only possible way.所以通过所有即将到来的道具是唯一可能的方法。

class BaseComponent extends React.Component {
      render() {
         const { className, onClick} = this.props;

         ...
      }
   }

class CustomTextField extends React.Component {
      render() {
         const { className, onKeyPress, value} = this.props;

         ...
      }
   }

const EnhancedBaseComponent = EnhanceComponent(BaseComponent);
const EnhancedTextComponent = EnhanceComponent(CustomTextField);

Now in this case EnhancedBaseComponent and EnhancedTextComponent both need different props, but since they are wrapped in EnhanceComponent.现在在这种情况下, EnhancedBaseComponentEnhancedTextComponent都需要不同的 props,但是因为它们被包装在 EnhanceComponent 中。 It won't know which props to pass.它不知道要通过哪些道具。 So spread it and send all the props coming to it.所以传播它并将所有道具发送给它。

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

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