属性“shouldComponentUpdate”不可分配给同一属性

Max*_*vis 7 javascript typescript reactjs

我在使用 TypeScript 时遇到错误shouldComponentUpdate

类型 'Hello' 中的属性 'shouldComponentUpdate' 不能分配给基类型中的相同属性Component<IProps, any, any>

在组件中:

import React, { Component } from 'react'

class Hello extends Component<IProps, any> {
  shouldComponentUpdate(nextProps: IProps) { // error here
    console.log(nextProps, 'nextProps')
  }

  ....// some code
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下我做错了什么吗?

Svi*_*lev 7

将 React 与 TypeScript 一起使用有点烦人,因为今天的最后一个没有包含所有必需的错误描述提示。因此,您的案例中的错误可能与方法未完成的return调用有关shouldComponentUpdate

尝试下一步,看看会发生什么:

  shouldComponentUpdate(nextProps: IProps) { // error here
    console.log(nextProps, 'nextProps')
    return true
  }
Run Code Online (Sandbox Code Playgroud)