反応成分にrefを追加する際に問題があります。以下はエラーメッセージです。反応参照が機能しない不変の違反:addComponentAsRefTo
invariant.js:39Uncaught Invariant Violation: 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
リサーチはこの原因が考えられ、いずれも問題ではないようです。
のみReactOwnerは
REFSこれが最も可能性の高い原因は反応すると私の不慣れに行うであることができます。私は、コンテナをレンダリングしてから、select要素をコンテナにレンダリングします。
コード
var React = require('react');
var first_container = React.createClass({
render: function() {
return (
<div id='first-container' className='jumbotron'>
<div className='row'>
<button type='button' className='btn btn-primary'>Submit Query</button>
</div>
</div>
);
}
});
var product_selections = React.createClass({
handleChange: function() {
console.log("hello");
},
componentDidMount: function(){
$(this.refs['product-select']).select2({
change: this.handleChange
});
},
render: function() {
return (
<div className='row'>
<br/>
<label htmlFor='product-dropdown'>
Product
<br/>
<select ref='product-select' className='js-example-basic-multiple js-states form-control' id='product-dropdown' multiple='multiple'>
</select>
</label>
</div>
);
}
});
あなたは、コンポーネントのrender()関数の外部で作成されている要素に参照を追加しようとしています。
refは、render関数内で定義されているため、問題ではないようです。このコンポーネントが別のコンポーネントに追加されていることが原因である可能性がありますか?リアクト
の複数コピー
npm ls | grep react ├─┬ [email protected] ├── [email protected] ├── [email protected] extraneous ├── [email protected] ├─┬ [email protected] │ ├─┬ [email protected]
はどちらか重複したパッケージがあるように思えないのですか?ここで何が起こっているのでしょうか?
複数の質問に同じ回答を投稿しないでください。質問が重複ではない場合は、*あなたの質問に対する回答を調整してください。* –