reactjs - React Higher Order Component (HOC) in typescript -
i trying write react hoc in typescript not getting definitions correct. not sure if trying accomplish possible.
here code
import * react 'react' export default function ajax<props, state>(innercomponent: typeof react.component): react.componentclass<props & state> { return class extends innercomponent<props & state,any> { constructor() { super() this.state = { request: 'initial' } } changerequest(newrequest) { this.setstate({request: 'loading'}) } render() { return <innercomponent {...this.props } {...this.state} changerequest={this.changerequest} /> } } }
if passing props , state child works. how can write definitions able pass additional props wrapped component? in case changerequest prop.
thanks
i able make work. not sure correct way. compiler not complaining , code works. compiler stop complaining had add props javascript objects.
here working code:
import * react 'react' export default function ajax<props, state> (innercomponent: typeof react.component): react.componentclass<props & state> { return class extends innercomponent<props & state, any> { constructor() { super() this.state = { request: 'initial' } this.changerequest = this.changerequest.bind(this) } changerequest(newrequest) { this.setstate({request: 'loading'}) } render() { return <innercomponent {...this.props} {...this.state} {...{onclick: this.changerequest}}/> } } }
Comments
Post a Comment