How to Get a React Component’s Element

By  on  

JSX is an amazing pseudo-language for React, and if I'm honest, it's what brought me to love React so much.  Using React without JSX is cumbersome and frustrating, while using JSX is such an easier way to express your code.  One drawback of JSX, however, is that it makes accessing component elements indirect, if not difficult.

The truth is that accessing a component's own elements is actually much easier than most think.  Let's look at how a component method can access its own DOM node with JavaScript:

Method 1:  react-dom

react-dom provides a findDomNode method for finding the component's node:

// Get ReactDOM
import ReactDOM from "react-dom";

// In your component method
class MyComponent extends Component {

    myMethod() {
        const node = ReactDOM.findDOMNode(this);
    }

}

With ReactDOM.findDOMNode(this), you can get the widget's main node, and from there you can use typical DOM methods:

const node = ReactDOM.findDOMNode(this);

// Get child nodes
if (node instanceof HTMLElement) {
    const child = node.querySelector('.someClass');
}

This mixes a bit of React and basic JavaScript DOM manipulation.

Method 2:  ref

Another method of getting DOM nodes is by using refs; an example usage is detailed in my React and autofocus post:

class MyComponent extends Component {

  // The element we want to retrieve
  _input: ?HTMLInputElement;

  // ....

  componentDidUpdate() {
    this._input.focus();
  }

  render() {
      return (
        <div>
            <input
              ref={c => (this._input = c)}
            />
        </div>
      );
    }
  }
}

Adding a ref attribute to the element you want a handle on is a more React-centric approach to getting a handle on an element.  Both strategies work well so choose whichever you prefer!

Recent Features

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    39 Shirts &#8211; Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

Incredible Demos

  • By
    Create a Simple Dojo Accordion

    Let's be honest:  even though we all giggle about how cheap of a thrill JavaScript accordions have become on the web, they remain an effective, useful widget.  Lots of content, small amount of space.  Dojo's Dijit library provides an incredibly simply method by which you can...

  • By
    MooTools 1.2 Tooltips: Customize Your Tips

    I've never met a person that is "ehhhh" about XHTML/javascript tooltips; people seem to love them or hate them. I'm on the love side of things. Tooltips give you a bit more information about something than just the element itself (usually...

Discussion

  1. If you’re on React 16.3 and up best to use React.createRef() (https://reactjs.org/docs/refs-and-the-dom.html#creating-refs)

  2. Rico

    There are new methods for creating a ref in React 16.3.

  3. Muhammad zubair

    i have a parent component in which two other components are defined .What i want is to access on component element in the second component to change it behavior.Help could be appreciated….

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!