继承React基础组件时,泛型写法

一、基础组件

export interface BaseProps {
    name: string;
}

export interface BaseState {
    age: number;
}

export class BaseComp<P extends BaseProps = any, S extends BaseState = any> extends React.Component<P, S> {
    state = {
        name: '123',
    } as S;
}

 

二、上层组件

 

interface BetterProps extends BaseProps {
    gender: 'man' | 'female';
}

interface BetterState extends BaseState {
    language: string;
}

class BetterComp extends BaseComp<BetterProps, BetterState> {
    state = {
        ...this.state,
        language: '中文',
    }
}

 

 

 

 

 
上一篇:flutter中的动画


下一篇:react-父子子孙组件嵌套-context