react-select: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs
Trying to integrate react-select into our application and we’re seeing the following error. There may be something in our workflow that is causing it. We’re using ReactDOM to render react components into our page.
Uncaught Error: 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 (details: https://fb.me/react-refs-must-have-owner).
An example:
// other code
function init() {
model.on('change', renderRecipientDisplay);
}
function logChange(val) { console.log("Selected: " + val); }
function renderRecipientDisplay() {
var ReactSelect = require('react-select');
var options = [{ value: 'one', label: 'One' }, { value: 'two', label: 'Two' }];
ReactDOM.render(
(
<ReactSelect name="form-field-name" value="one" options={options} onChange={logChange} />
),
$el.find('.routing-recipient-display')[0]
);
}
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 19
- Comments: 34 (1 by maintainers)
We ended up giving up on
react-selectfor the time being.Just for an information, I receive this error when I add a new dependency to the project. If I delete
node_modulesand reinstall all the dependencies the error does not occur again.We had this same issue but found a fix that has not been mentioned thus far. In our webpack setup we had a DLLs config with several entry points that was responsible for outputting 5-6 DLLs to be referenced in the main config. ‘react-select’ was in one and ‘react-modal’ was in another. Upon further inspection I realized that each of the DLL bundles was getting it’s own copy of react. The bug only surfaced when a react-select box was nested inside of a react modal, but both libraries worked independently.
Solution was to have a multi-layered dll build process where a dll bundle containing just react and friends is created first, then referenced by the “level 2” dll bundles that contain things like react-select, then all the resulting dll bundles are referenced by the main build file.
Use the webpack analyze tool to inspect the stats from your bundles, it should provide some insight into how and why webpack is inserting multiple bundles.
Another solution that has worked in the past for me with the ‘multiple copies of react’ issue is to use the resolve section of the webpack config to point to a particular single copy of react.
I am running into this issue too, using browserify-shim with the following external config:
(As we had to load React on the page itself because other code on the page outside of the component I am trying to build relies on it). That was all working fine, until we did the following:
Has anyone resolved or worked around this issue while still using browserify-shim for React? It seems like browserify should be using the same configuration when resolving the requires down in a node_module package.
Should I give up???
I spent a couple of hours building this use case in isolation and after that, I decided the release a reusable component. However, after hours and hours and days trying to get this to work I’m finally in the step of making a decision…
I based my example on the sample from http://jedwatson.github.io/react-select/.
Running in isolation: Works
Running as a Shared Component
The component above then was placed in an isolated NPM Module/component and published to our private NPM registry. Then, when trying to see the component, I get the following error…
Running as a Share Library: Arrows keys partially work…
As a consequence, the UI can’t render the properties right, but I can still use the up-down arrows to select my options, but without the nice UI experience…
Still work although it does not render properly…
Events don’t work anymore
No duplicate react Dependency
same as cubbuk. I deleted node_modules and reinstalled all dependencies. The error went away
I tried with a simple component (cjsx syntax)
But I still get the same error. I tried to remove all
require('react')andrequire('react-dom')fromreact-selectbut error persists. I think it is related to the usage of refs in your code maybe? I tried both versions 0.9.1 and 1.0.0-beta9.I will have to give up on
react-selectsame as @mockdeep unless someone has some suggestions?I encounter this problem few days ago with the same message as you got from the console…
The reason was React module was loaded twice. I had an html file that contains
and subcontract.js is generated by webpack. The problem was one of the modules inside subcontract has a dependency to React module and webpack has also bundled that into the compiled subcontract.js. Therefore two React module was loaded in the page…