1. 程式人生 > >React組件繼承的由來

React組件繼承的由來

繼承 scribe des ring rop eve 組件 span sha

沒有顯式繼承的時候我們這麽寫:

import * as React from "react";

export interface HelloProps { compiler: string; framework: string; }

export const Hello = (props: HelloProps) => <h1>Hello from {props.compiler} and {props.framework}!</h1>;

我們把它寫的更像類一些:

import * as React from "react";

export interface HelloProps { compiler: string; framework: string; }

// ‘HelloProps‘ describes the shape of props.
// State is never set so we use the ‘{}‘ type.
export class Hello extends React.Component<HelloProps, {}> {
    render() {
        return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
    }
}

  

React組件繼承的由來