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
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Create a 3D Panorama Image with A-Frame

    In the five years I've been at Mozilla I've seen some awesome projects.  Some of them very popular, some of them very niche, but none of them has inspired me the way the MozVR team's work with WebVR and A-Frame project have. A-Frame is a community project...

  • By
    Create a Dynamic Table of Contents Using MooTools 1.2

    You've probably noticed that I shy away from writing really long articles. Here are a few reasons why: Most site visitors are coming from Google and just want a straight to the point, bail-me-out ASAP answer to a question. I've noticed that I have a hard time...

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!