【发布时间】:2018-05-01 19:11:36
【问题描述】:
我在我的 ruby on rails 项目中使用 react-rails gem。我尝试添加对我的 DOM 元素的引用。这是我的组件:
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>
);
}
};
当我尝试在浏览器中加载页面时,控制台中出现以下消息:
TypeError: React.createRef is not a function. (In 'React.createRef()', 'React.createRef' is undefined).
【问题讨论】:
-
你用的是最新版本的 react 吗?
-
@GautamNaik 是的
标签: javascript ruby-on-rails reactjs