Typescript with React - use HOC on a generic component class

后端 未结 5 1979
别那么骄傲
别那么骄傲 2020-12-30 05:14

I have a generic React component, say like this one:

class Foo extends React.Component, FooState> {
    constructor(props: F         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 05:32

    You can wrap your component which is created from a HOC into another component. It would look something like this:

    class FooWithTd extends React.Component> {
         private Container: React.Component & HOCResultType>; 
    
         constructor(props:SomeType){
              super(props);
              this.Container = withTd(Foo);
         }
    
         render() {
              return ;
         }
    }
    

    Remember, you probably don't want the HOC inside your render function because it means that the component will be recreated every each render.

提交回复
热议问题