Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.createRef is not a function in react-rails

A am using react-rails gem in my ruby on rails project. I try to add reference to my DOM element. This is my component:

class NewItem extends React.Component {
  constructor(props) {
    super(props);
    this.name = React.createRef();
  }
  handleClick() {
    var name  = this.name.value;
    console.log(name);
  }
  render() {
    return (
      <div>
        <input ref={this.name} placeholder='Enter the name of the item' />
        <button onClick={this.handleClick}>Submit</button>
      </div>
    );
  }
};

When i try to load the page in browser i have this message in console: TypeError: React.createRef is not a function. (In 'React.createRef()', 'React.createRef' is undefined).

like image 506
Herman Eyd Avatar asked Feb 04 '23 00:02

Herman Eyd


1 Answers

update react to 16.3 React.createRef() this API is added on react 16.3 checkout this https://github.com/facebook/react/pull/12162

like image 127
贺祖辉 Avatar answered Feb 06 '23 13:02

贺祖辉