我试图ref使用React 使用该属性.我的浏览器出现了一个奇怪的错误,我无法弄清问题是什么.任何人都可以向我解释为什么我会收到此错误:
错误:不变违规:addComponentAsRefTo(...):只有ReactOwner可以有refs.这通常意味着您尝试将ref添加到没有所有者的组件(即,不是在另一个组件的
render方法内创建).尝试在一个新的顶级组件中渲染此组件,该组件将保存ref.
当我有这个代码时:
/**
* @jsx React.DOM
*/
(function(){
var react = require('react');
var App = react.createClass({
render: function() {
return (
<h1 ref="myRef">This is a test</h1>
);
}
});
react.render(
<App />,
document.body
);
}());
Run Code Online (Sandbox Code Playgroud)
你的代码是正确的。
工作 jsFiddle:http://jsfiddle.net/reactjs/69z2wepo/
var App = React.createClass({
render: function() {
return (
<h1 ref="myRef">This is a test</h1>
);
}
});
React.render(
<App />,
document.body
);
Run Code Online (Sandbox Code Playgroud)
根据错误消息,您正在将 ref 放置在未拥有的元素上,但在您提供的代码中,该元素h1由App. 你的代码和上面粘贴的不一样吗?
注意(来自文档):
In React, an owner is the component that sets the props of other components ...
It's important to draw a distinction between the owner-ownee relationship and the parent-child relationship.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3546 次 |
| 最近记录: |