简体   繁体   中英

ReactJs / Javascript - Can a HOC read the parameter component's props?

I have a Higher Order Component that receives another component as a parameter:

HOC

export default function HOC(Comp) {
  return class extends Component {

  doSomething() {
    const temp = // the Comp's clientId prop???
  }
  ........

 }
}   

Sub Component

@HOC
export default class SubComponent extends Component {    
  .....    

 static proptypes = {
  clientId: PropTypes.string.isRequired
 };

.......    
}

Question:

Is it possible in the scenario above for the HOC to be aware of SubComponent's clientId property in its arguements and if so, how can I make the HOC aware of it for my doSomething function?

由于实际上是HOC接收道具(或函数返回的组件),因此您可以使用this.props访问:

const temp = this.props.clientId;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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