網站搜尋

javascript - 在 TSX 文件中:屬性 'createRef' 不存在於類型 '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);
}

答案#1

看起來你使用的 @types/react 版本已過時。最新版本 16.3.14 已經有 createRef 的類型定義。