Error response

I am trying to use the ref property using React. I am getting a strange error in my browser and I cannot figure out what the problem is. Can someone explain to me why I get this error:

Error: Invariant violation: addComponentAsRefTo (...): only ReactOwner can have links. This usually means that you are trying to add ref to a component that does not have an owner (i.e. it was not created inside another render method). Try visualizing this component inside the new top-level component that will contain the link.

when i have this code:

 /** * @jsx React.DOM */ (function(){ var react = require('react'); var App = react.createClass({ render: function() { return ( <h1 ref="myRef">This is a test</h1> ); } }); react.render( <App />, document.body ); }()); 
+6
source share
2 answers

The correct code.

JsFiddle work: http://jsfiddle.net/reactjs/69z2wepo/

 var App = React.createClass({ render: function() { return ( <h1 ref="myRef">This is a test</h1> ); } }); React.render( <App />, document.body ); 

According to the error message, you place a link to an item that does not belong to you, but in the code that you specified, h1 belongs to the App . Is your code different from what you pasted above?

Note ( from documents ):

 In React, an owner is the component that sets the props of other components ... It important to draw a distinction between the owner-ownee relationship and the parent-child relationship. 
0
source

This answer can help you visit , carefully check your code to solve these two questions, my error is caused by the latter.
In my code I wrote require("React") require("React-dom") , actually it is require('react') , I changed my code, it worked. All errors are caused by two factors. Just check your code completely.

0
source

Source: https://habr.com/ru/post/979034/


All Articles