【发布时间】:2018-10-25 20:31:50
【问题描述】:
我有一个简单的react 组件,其中包含一个表单:
var AddAppts = React.createClass({
handleClick: function() {
var title = this.refs.title.getDOMNode().value;
....
var appt = {
heading: title
...
}
CalendarActions.addAppointment(appt);
},
render: function() {
return (
<div>
<label>Description</label>
<input ref="title"></input>
...
</div>
);
}
});
module.exports = AddAppts;
我正在尝试在另一个react 组件中render 这个组件:
var AddAppt = require('./AddAppts');
render: function() {
return (
<div>
<AddAppt />
</div>
);
}
但我收到此错误:
Uncaught Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's `render` method). Try rendering this component inside of a new top-level component which will hold the ref.
我已经用谷歌搜索了它,但无法弄清楚我需要做什么来解决这个问题。
【问题讨论】:
-
这是 2 个不同的文件。当我导入文件时,我给它起了一个不同的名字。
-
我试图复制我认为是您的设置。 this 很接近吗?
-
您能否说明您使用的是哪个 React 版本?你找到问题所在了吗?
-
你找到这个错误的含义了吗?
-
与此同时,Facebook 已添加指向错误消息的链接:gist.github.com/jimfb/4faa6cbfb1ef476bd105。请务必查看 cmets 以获得可能的解决方案。
标签: reactjs