Reactjs 如何在React中将属性从HOC注入根元素?
我想使用HOC将className添加到组件的根目录中Reactjs 如何在React中将属性从HOC注入根元素?,reactjs,typescript,Reactjs,Typescript,我想使用HOC将className添加到组件的根目录中 const Component = () => <div>...</div>; const ComponentWithHello = withHello(Component); 类型“Component”不是构造函数类型。您试图扩展类型React.Component(即,如果调用new Component,将得到什么),而不是类本身 WrappedComponent应为React.ComponentType类
const Component = () => <div>...</div>;
const ComponentWithHello = withHello(Component);
类型“Component”不是构造函数类型。
您试图扩展类型React.Component
(即,如果调用new Component
,将得到什么),而不是类本身
WrappedComponent
应为React.ComponentType
类型。React.ComponentClass
这也可以使用<代码>函数withHello(WrappedComponent:React.ComponentClass){return class Enhancer extensed WrappedComponent{}@Harish-True,但是ComponentClass
将只接受类组件,而ComponentType
将同时接受类组件和功能组件。因此,我建议使用后者。
function withHello<T>(WrappedComponent: React.Component<T>) {
return class Enhancer extends WrappedComponent {} //Error here
}