基于TypeScript的React组件中通用参数的第二个参数代表什么?

3

我正在考虑使用React和Typescript。在官方Typescript的网站上,他们的一个指南中有这样一组组件代码:

import * as React from "react";

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

export class Hello extends React.Component<HelloProps, {}> {
    render() {
        return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
    }
}

在这一行:
export class Hello extends React.Component<HelloProps, {}>

我知道泛型参数中的第一个参数可能是指具有所有属性的属性接口(this.props)。
在泛型参数中,{}的第二个参数代表什么?它看起来像一个对象,但它是做什么的?我可以在里面放什么?我怎么使用它?
我尝试搜索了解这个问题,但关于React和Typescript的信息并不多。
1个回答

3
他是指React组件的状态。 例如:
export class Hello extends React.Component<HelloProps, {name: string}> {
    render() {

        return <h1>Hello {this.state.name}</h1>;
    }
}

1
哦!这是否意味着我也可以像示例中的 props 一样将我的状态放在接口中? - Carven
这里有关于此的更多信息:https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/react - Quentin Roy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接