Ошибка типа: __WEBPACK_IMPORTED_MODULE_0_react ___ default.a.createRef не является функцией - PullRequest
0 голосов
/ 01 июня 2018

Я новичок в React.js и только сейчас я изучал концепцию ref в React.У них есть новый createRef API в V16.3.Я пытался узнать это из РЕАКТИВНЫХ ДОК. , как это -

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

}

И я получал эту ошибку -

Ошибка типа: __WEBPACK_IMPORTED_MODULE_0_react ___ default.a.createRef не является функцией

Вот снимок экрана - enter image description here

Ответы [ 2 ]

0 голосов
/ 07 ноября 2018

Если вы не можете обновить свою версию реакции, вы можете использовать устаревшие ссылки на строки (https://reactjs.org/docs/refs-and-the-dom.html#legacy-api-string-refs)

. Вы задаете строку для атрибута ref:

<input
    type="text"
    ref="textInput" />

И получаете к ней доступ следующим образом.:

this.refs.textInput.focus();

И не забудьте удалить эту часть:

this.textInput = React.createRef();
this.focusTextInput = this.focusTextInput.bind(this);
0 голосов
/ 01 июня 2018

У вас, похоже, не установлена ​​правильная версия реакции

Сделайте следующее:

npm install --save react@16.4.0 react-dom@16.4.0
...