React组件继承,泛型写法

react

一、单层继承

1)基础组件

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;

}

2)上层组件

interface BetterProps extends BaseProps {

gender: 'man' | 'female';

}

interface BetterState extends BaseState {

language: string;

}

class BetterComp extends BaseComp<BetterProps, BetterState> {

state = {

...this.state,

language: '中文',

}

}

 二、组合继承

1)基础组件 

 

以上是 React组件继承,泛型写法 的全部内容, 来源链接: utcz.com/z/382272.html

回到顶部