Reactjs 扩展React.Component类

Reactjs 扩展React.Component类,reactjs,typescript,Reactjs,Typescript,我最近开始与React合作,了解框架的工作原理 我在下面有两段代码,只是想知道它们之间的区别是什么(当然我不是在问语法上的区别)以及为什么其中一段给出了错误 这一个有效 interface Square { value:String; } class Square extends React.Component<Square,{}> { } 我在网上看到了许多例子,它们扩展了React.Component来编写新组件,我想我在这里遗漏了一些东西 因为您正在使用TypeScr

我最近开始与React合作,了解框架的工作原理

我在下面有两段代码,只是想知道它们之间的区别是什么(当然我不是在问语法上的区别)以及为什么其中一段给出了错误

这一个有效

interface Square {
  value:String;
}

class Square extends React.Component<Square,{}> {

}

我在网上看到了许多例子,它们扩展了React.Component来编写新组件,我想我在这里遗漏了一些东西

因为您正在使用TypeScript

您在internet上看到的代码
classxxx.Component
只是ES6代码

下面是用TypeScript编写的简单React代码:

interface SomeProps {
    blabla: string;
}

class SomeComponent extends React.Component<SomeProps, any> {
    constructor(props: SomeProps) {
        super(props);
    }

    render() {
        return <h1>{this.props.blabla}</h1>;
    }
}
interface SomeProps{
布拉布拉:字符串;
}
类SomeComponent扩展了React.Component{
构造器(道具:SomeProps){
超级(道具);
}
render(){
返回{this.props.blabla};
}
}
interface SomeProps {
    blabla: string;
}

class SomeComponent extends React.Component<SomeProps, any> {
    constructor(props: SomeProps) {
        super(props);
    }

    render() {
        return <h1>{this.props.blabla}</h1>;
    }
}