我想在react 组件里面用装饰器实现给组件绑定 pubsub 的功能
装饰器实现如下:
// withPubsub.tsx
class PubsubClass {/* 具体实现省略,问题不是出在这个部分 */}
export default function withPubsub<Props, State> (Comp: React.ComponentClass<Props>) {
return class extends React.Component<Props, State> {
private pubsub: typeof PubsubClass
constructor (props: Props) {
super(props)
this.pubsub = PubsubClass
}
componentWillUnmount () {
this.pubsub.off()
}
render () {
return <Comp { ...this.props } /> // 这里编译出错,提示找不到 Comp
}
}
}