TypeError:__ WWEPACK_IMPORTED_MODULE_0_react ___ default.a.createRef不是函数

coo*_*per 6 javascript ref reactjs

我是React.js的新手,刚才我正在学习refReact 的概念.他们在V16.3中有新的createRef API.我试图从REACT DOC这样学习这个 -

import React from "react";

export class MyComponent extends React.Component {

constructor(props) {
    super(props);
    // create a ref to store the textInput DOM element
    this.textInput = React.createRef();
    this.focusTextInput = this.focusTextInput.bind(this);
}

focusTextInput() {
    // Explicitly focus the text input using the raw DOM API
    // Note: we're accessing "current" to get the DOM node
    this.textInput.current.focus();
}

render() {
    // tell React that we want to associate the <input> ref
    // with the `textInput` that we created in the constructor
    return (
        <div>
            <input
                type="text"
                ref={this.textInput} />

            <input
                type="button"
                value="Focus the text input"
                onClick={this.focusTextInput}
            />
        </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

}

我得到了这个错误 -

TypeError:__ WWEPACK_IMPORTED_MODULE_0_react ___ default.a.createRef不是函数

这是截图 - 在此输入图像描述

sup*_*a28 9

您似乎没有安装正确版本的react

做这个 :

npm install --save react@16.4.0 react-dom@16.4.0
Run Code Online (Sandbox Code Playgroud)

  • 我对“react”和“react-dom”都使用“^16.13.1”,但这并没有解决问题。 (3认同)