我想在打字稿中寫一個React HOC,但我沒有得到正確的定義。我不知道我是否有可能實現自己的目標。在打字稿中反應高階組件(HOC)
這裏是我的代碼
import * as React from '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}
/>
}
}
}
如果我只是路過的道具和狀態,它的工作原理孩子。但是我如何編寫定義以便能夠將其他道具傳遞給包裝組件?在這種情況下,changeRequest支柱。
謝謝
我不知道哪裏出了問題。被包裝的組件需要知道所有的屬性,你不應該在HOC中擴展它的屬性定義。 – niba