var MyComponent = React.createClass({
handleClick: function() {
this.refs.myTextInput.focus();
},
render: function() { return (
<div>
<input type="text" ref="myTextInput" />
<input type="button" value="Focus the text input" onClick={this.handleClick} />
</div>
);
}
});
ReactDOM.render(
<MyComponent />,
document.body
);通過用this.refs.myTextInput.focus();,可是頁面沒有效果,提示錯誤:Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that
was not created inside a component's `render` method, or you have multiple copies of React loaded
(details: https://fb.me/react-refs-must-have-owner).求指導,謝謝
react中使用ref報錯
藍山帝景
2018-09-19 09:09:40