在 TSX 文件中:属性 'createRef' 在类型 'typeof React' 上不存在

In TSX file : Property 'createRef' does not exist on type 'typeof React'

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

其他解决方法是使用查询选择器,但我认为这不是在 React 中做事的好方法。

这里是构造函数

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);
}

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

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

yarn upgrade @types/react-dom^16.3.0