【问题标题】:extending React.Component class扩展 React.Component 类
【发布时间】:2017-01-17 06:13:33
【问题描述】:

我最近开始使用 React 并了解框架的工作原理,

我在下面有两段代码,只是想知道它们之间有什么区别(当然我不是在问语法差异)以及为什么其中一个会出错。

这个有效

interface Square {
  value:String;
}

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

}

这会出现以下错误

[ts] 通用类型“组件”需要 2 个类型参数。

class Square extends React.Component {}

我在网上看到很多扩展 React.Component 以编写新组件的示例,我想我在这里遗漏了一些东西。

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    因为您使用的是 TypeScript。

    你在网上看到的class xxx extends React.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>;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 2019-09-12
      • 2022-11-20
      • 2017-04-03
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多