Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'Only a ReactOwner can have refs.' mean?

Tags:

reactjs

I have a simple react component with a form in it:

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; 

I am trying to render this component in another react component:

  var AddAppt = require('./AddAppts');    render: function() {     return (       <div>         <AddAppt />       </div>     );   } 

but I get this error:

 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. 

I have googled it, but cannot figure out what I need to do to fix this issue.

like image 664
jhamm Avatar asked Feb 14 '15 19:02

jhamm


People also ask

What are refs for?

Refs are a function provided by React to access the DOM element and the React element that you might have created on your own. They are used in cases where we want to change the value of a child component, without making use of props and all.

Why refs are not recommended?

It is a general rule of thumb to avoid using refs unless you absolutely have to. The official React documentation outlined only three possible use cases where refs are entirely considered useful for lack of better alternatives: Managing focus, text selection, or media playback. Triggering imperative animations.

Why do we need refs?

There are a few good use cases for refs: Managing focus, text selection, or media playback. Triggering imperative animations. Integrating with third-party DOM libraries.

Can I pass ref as prop?

Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.


2 Answers

This is FYI for people using react and redux-devtools who are getting OP's message. Your structure looks like

project   client   node_modules     react     redux-devtools       node_modules         react 

The code in client will require the first react module; that in redux-devtools will require the other react. The react module keeps state and assumes it has all the state.

You get the OP's message because your state is split between the 2 react modules. This situation is what I believe @asbjornenge refers to.

I was bundling the client code with webpack, so I used that to handle the issue. You can force all require and import to always use the first react by adding the following to webpack.config.js

module.exports = {   ...   resolve: {     alias: {       'react': path.join(__dirname, 'node_modules', 'react')     },     extensions: ['', '.js']   },   ... ); 

I have not looked into what I would need to do if the situation occurred with unbundled code running on node.

like image 66
JohnSz Avatar answered Oct 12 '22 04:10

JohnSz


I encountered this error when a component module I was using had it's own react dependency installed. So I was using multiple versions of React.

Make sure NOT to list react under dependencies in your package.json.
This is why we have peerDependencies ;-)

like image 20
asbjornenge Avatar answered Oct 12 '22 03:10

asbjornenge