【问题标题】:In TSX file : Property 'createRef' does not exist on type 'typeof React'在 TSX 文件中:“typeof React”类型上不存在属性“createRef”
【发布时间】:2018-10-25 03:29:02
【问题描述】:

我需要组件的引用。刚从 jsx 转移到 tsx,找不到解决这个问题的方法。

其他解决方法是使用查询选择器,但我相信这不是一种很好的反应方式。

这里是构造函数

constructor(props) {
    super(props);
    this.state = {
        data: initialData,
        showNewListForm: false,
        whichButtonClicked: undefined,
        background: props.background || "#23719f"
    };

    this.divContainerRef = React.createRef();
    console.log("kanban reference : " + React.createRef().current);

    this.handleDragStart = this.handleDragStart.bind(this);
    this.handleDragEnd = this.handleDragEnd.bind(this);
    this.handleLaneDragEnd = this.handleLaneDragEnd.bind(this);
    this.handleLaneDragStart = this.handleLaneDragStart.bind(this);
    this.onCardAdd = this.onCardAdd.bind(this);
    this.onCardClick = this.onCardClick.bind(this);
    this.addNewListHandler = this.addNewListHandler.bind(this);
    this.containerRefResolver = this.containerRefResolver.bind(this);
    this.isBoardPresent = this.isBoardPresent.bind(this);
}

【问题讨论】:

  • 您是否正确导入了React?
  • import * as React from 'react';

标签: javascript reactjs typescript jsx tsx


【解决方案1】:

您的@types/react 版本似乎已过期。最新版本 16.3.14 具有 createRef 的类型定义。

【讨论】:

  • 宾果游戏!!!你的解决方案奏效了!非常感谢。我正在干预代码,并找到了另一种解决方案,一种解决方法,(React as any).createRef() 也可以。
【解决方案2】:

在我的 Typescript 代码库中,我必须更新 react-dom 类型:

yarn upgrade @types/react-dom^16.3.0

【讨论】:

  • 16.3.0 的 npm 或 yarn 上没有 react-dom 类型的标记版本。
  • 你是对的。我假设 yarn 将其解析为下一个语义版本
猜你喜欢
  • 2020-10-04
  • 1970-01-01
  • 2021-01-09
  • 2019-09-14
  • 1970-01-01
  • 2022-08-05
  • 2020-08-19
  • 2017-11-06
  • 2018-11-09
相关资源
最近更新 更多