Javascript 基于Typescript的React组件的泛型参数中的第二个参数代表什么?
我正在考虑使用React和Typescript。在官方的Typescript上,他们的一个指南有一套组件代码:Javascript 基于Typescript的React组件的泛型参数中的第二个参数代表什么?,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我正在考虑使用React和Typescript。在官方的Typescript上,他们的一个指南有一套组件代码: import * as React from "react"; export interface HelloProps { compiler: string; framework: string; } export class Hello extends React.Component<HelloProps, {}> { render() { r
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>;
}
}
import*as React from“React”;
导出接口HelloProps{compiler:string;framework:string;}
导出类Hello扩展React.Component{
render(){
从{this.props.compiler}和{this.props.framework}返回Hello!;
}
}
在这一行:
export class Hello extends React.Component<HelloProps, {}>
导出类Hello扩展React.Component
我知道泛型参数中的第一个参数可能是指包含我所有属性的属性接口(this.props
)
泛型参数中{}
的第二个参数代表什么?它看起来像一个物体,但它做什么,我能在里面有什么?我怎么能用它呢
我已经尝试过对此进行搜索,但是没有太多关于React with Typescript的信息。它指的是React组件的状态。 e、 g
导出类Hello扩展React.Component{
render(){
返回Hello{this.state.name};
}
}
哦!这是否意味着我也可以像示例中的道具一样,将状态放在接口中?更多信息
export class Hello extends React.Component<HelloProps, {name: string}> {
render() {
return <h1>Hello {this.state.name}</h1>;
}
}