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.

[0.14] this.refs... not returning element from specific components..

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
m4tthumphreycommented, Oct 8, 2015

@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 call this.refs.ref.getDOMNode() to retrieve the element; you just need to call this.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 so ReactDOM.findDOMNode(this.refs.ref).

So if you have something like the following in your code

function componentDidMount: function() {
  var el1 = this.refs.ref1.getDOMNode();
  var el2 = this.refs.ref2.getDOMNode();

  // ...
},

function render() {
  return (
    <div>
      <input ref="ref1" />
      <MyComponent ref="ref2" />
    </div>
  );
}

you need to change it like so:

function componentDidMount: function() {
  var el1 = this.refs.ref1;
  var el2 = ReactDOM.findDOMNode(this.refs.ref2);

  // ...
},

function render() {
  return (
    <div>
      <input ref="ref1" />
      <MyComponent ref="ref2" />
    </div>
  );
}

Hope that clears it up. If you haven’t updated your code before 0.15, it will error.

0reactions
TonyFranciscommented, Oct 9, 2015

@m4tthumphrey solved my problem child reference not formed at begining so needed a time delay so refs to be available

Read more comments on GitHub >

github_iconTop 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 >

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