You are probably here because you got the following error messages:
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.
This usually means one of two things:
- You are trying to add a
ref
to an element that is being created outside of a component's render() function. - You have multiple (conflicting) copies of React loaded (eg. due to a miss-configured NPM dependency)
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.
Bower does a good job of deduplicating dependencies, but NPM does not. If you aren't doing anything (fancy) with refs, there is a good chance that the problem is not with your refs, but rather an issue with having multiple copies of React loaded into your project. Sometimes, when you pull in a third-party module via npm, you will get a duplicate copy of the dependency library, and this can create problems.
If you are using npm... npm ls
or npm ls | grep react
might help illuminate.
I finally have been able to solve this for myself. I'm using Rails and was using
react-rails
for React integration. I moved on to this gem : https://github.com/netguru/react_webpack_railsI guess my problem was a bit specific though. My problem was to import 'react-color' components and I kept getting the invariant error. I knew there was an issue specific to 'react-color' because 'react-css' would have a dependency with another React version and then I would have 2 versions.
But even trying with older versions not depending on 'react-css', I kept having the error even though I checked there was only one React version in npm.
I noticed they fixed it recently by depending on mochas instead, so I changed my dependencies to include the github version
"react-color": "git+https://github.com/casesandberg/react-color/"
And now it finally works ! Hope this helps, I think going to webpack helped since it still didn't work with the older version of 'react-color'