question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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).

See original GitHub issue

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).

This has been talked about; the answer: ‘don’t run two copies of React!’. I got it. Here’s the thing: I’m not running two copies of React. I’m building a React component npm module that is installed in a larger project. Within my module, I’m including React as a peer dependency and it’s marked as an external in my webpack.config.js. This has actually been working fine all the time, with one exception: when my components have refs.

I’ve gone so far as to use a document.querySelector() (yikes!) to get rid of that error message. There has to be a better way. I’m opening an issue now, though, because I want to use the react-widgets package and it uses refs. This is breaking my project.

Can’t React be smart enough to associate these refs with the running copy of React? Am I missing something?

Thanks

-Ben

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
benwiley4000commented, Nov 24, 2015

I’m fairly certain it’s a problem only with npm link, though I’m having trouble producing it on one of the two modules that were experiencing this error before, so there may have been compounding factors related to outdated node/npm versions. For now, consider that my module (react-component-x) relies on a peerDependency, react-widgets, which uses refs. My project, react-project-x requires react-component-x. When react-component-x is installed via npm link, the error above is displayed (I just tested this a moment ago). When I run npm unlink react-component-x inside of react-project-x and then install react-component-x from its remote git repository, the error goes away.

1reaction
benwiley4000commented, Nov 24, 2015

Update: This issue has gone away for me. I’m not 100% sure what solved the problem, but I think it may be thanks to upgrading npm to v3.x and node to v4.x – and/or not using npm link.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refs Must Have Owner Warning - React
You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies...
Read more >
What does 'Only a ReactOwner can have refs.' mean?
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...
Read more >
Fixing the Jest Error, "Invariant Violation
You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies...
Read more >
Troubleshooting | React Redux
Invariant Violation : addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a ...
Read more >
How to use React createRef - LogRocket Blog
Learn how to use the React.createRef() method and useRef Hook in React to simplify creating refs and interact with the HTML DOM.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found