[0.14] this.refs... not returning element from specific components..
See original GitHub issueI’m not sure what the cause or pattern is yet but some of my components are causing this.refs.name
to return the following object from within componentDidMount()
. Other components are fine however.
Edit: See the bottom of this post for a possible cause.
R…s.c…s.Constructor {props: Object, context: Object, refs: Object, updater: Object, state: Object…}
I can post the whole object if necessary…
A snippet from my component:
componentDidMount: function() {
console.log(this.refs.number);
console.log(this.refs.label);
},
// ...
render: function() {
// ...
return (
<tr>
<td><TextField ... ref="label" /></td>
<td><TextField ... ref="number" /></td>
</tr>
);
}
As I’m typing this I’ve noticed that this is using a custom component TextField
when setting the ref, maybe this is the problem and could be a bug; assigning a ref to a custom component type? Note that this.refs.name.getDOMNode()
works correctly albeit with the 0.14 warning.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5
Top Results From Across the Web
How Refs works on react 0.14 - Stack Overflow
From my understanding, ref should be a callback method that receives the referenced element. In this case, input .
Read more >[SOLVED] This.$refs.key returns undefined when it really is
Try adding a breakpoint after the console.log() and you will be able to see that $refs has not been populated with any referenced...
Read more >Refs don't work on Stateless Components · Issue #4936 - GitHub
If we in the future allow multiple components to be returned from render, findDOMNode won't work. Likewise it might return null if a...
Read more >Understanding refs in Vue - LogRocket Blog
Refs are Vue.js instance properties used to register a reference to HTML elements or child elements in the template of your application.
Read more >Use React ref to Get a Reference to Specific Components
[02:01] Ref actually returns the node that we're referencing, here, I can say this.refs.b.value. Now, in the browser, when I type in the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@TonyFrancis Wherever you are using
this.refs.ref.getDOMNode()
you need to update your code to use one of the following methods based on type of the element you are calling it on.Method 1: If the ref points to a standard component (DOM node, such as
input
,select
,div
etc) then you do not need to callthis.refs.ref.getDOMNode()
to retrieve the element; you just need to callthis.refs.ref
.Method 2: If the ref points to a composite component (a custom component you have created yourself) you need to use the new
ReactDOM
module like soReactDOM.findDOMNode(this.refs.ref)
.So if you have something like the following in your code
you need to change it like so:
Hope that clears it up. If you haven’t updated your code before 0.15, it will error.
@m4tthumphrey solved my problem child reference not formed at begining so needed a time delay so refs to be available