reactjs - How is scope defined in Higher Order Components in React? -
a class in js still function. understanding when create wrapped1 have 'this' tied app since lives inside of wrapped1 has own scope. can't figure out i'm wrong.
let higherordercomp = (component) => class extends react.component{ construstor(props){ super(props); this.state = { count: 0 }; } componentdidmount(){ setinterval(()=>{ this.setstate( count: this.state.count + 1 ); }, 1000) } render(){ return <component {...this.props} {...this.state}> } } class comp1 extends react.component{ render(){ return( <div> <p>comp1</p> {this.props.count} </div> ); } } let wrapped1 = higherordercomp(comp1); class app extends react.component{ constructor(props){ super(props); } render(){ return( <div> <wrapped1/> </div> ); } } reactdom.render(<app/>, document.getelementbyid('app'));
an instance of wrapped1
created inside app
, has no knowledge of app
. if need access app
instance within wrapped1
, should pass data need props.
Comments
Post a Comment