javascript - 属性 'shouldComponentUpdate' 不可分配给同一属性

标签 javascript reactjs typescript

我在使用 shouldComponentUpdate 的 TypeScript 中遇到错误:

Property 'shouldComponentUpdate' in type 'Hello' is not assignable to the same property in base type 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
}

谁能解释一下我做错了什么?

最佳答案

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

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

  shouldComponentUpdate(nextProps: IProps) { // error here
    console.log(nextProps, 'nextProps')
    return true
  }

关于javascript - 属性 'shouldComponentUpdate' 不可分配给同一属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53632966/

相关文章:

javascript - React JS,总分

javascript - React Native - 如何在其他组件之上创建和渲染组件

javascript - 如何获得有关动态附加和加载 IFRAME 的通知?

javascript - 在 React 中使用 onClick 事件处理程序

JavaScript - 根据依赖树排序

javascript - 使用getjson读取json数据

javascript - es6 传播运算符如何处理对象到 Prop 的转换?

javascript - 使用 Jest 测试 module.hot

javascript - TypeScript - 基于枚举比较的 Switch 语句直接进入 DefaultCase

javascript - 如何包含/不包含 typescript 文件