Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use Ref Callbacks to Measure React Component Size

DZone's Guide to

Use Ref Callbacks to Measure React Component Size

Did you know that you can use ref callbacks to measure the rendered size of React components? It's a neat little trick. Read on to learn how to use it!

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

You can use ref callbacks to measure the rendered size of React components, did you know? It's a neat little trick.

Here's how it works:
1. React renders your component.
2. Browser layout engine does its thing.
3. ref callback fires.
4. Use getBoundingClientRect to measure element size.
5. Use this info for whatever you want.

We used this trick in this Dynamic SVG accordion example and in this Tiny React & D3 flamegraph tutorial. That's because layouting in SVG is hard, and you have to do everything yourself.

Using ref callbacks to measure your elements is a little less useful in the modern HTML + CSS world. You can use flexbox and css-grid so you never need to know what you're dealing with.

And yet when push comes to shove, sometimes you just really need your code to know the size of an element.

A minimal size reporting component looks like this:

class ReportSize extends React.Component {
  refCallback = element => {
    if (element) {
      this.props.getSize(element.getBoundingClientRect());
    }
  };

  render() {
    return (
      <div ref={this.refCallback} style={{ border: "1px solid red" }}>
        {faker.lorem.paragraphs(Math.random() * 10)}
      </div>
    );
  }
}

The render method outputs a <div> with a ref callback and a red border. Inside, we use faker to generate up to 10 random paragraphs.

After React places this element, it calls refCallback with a reference to the rendered DOM node. We can then use getBoundingClientRect to measure its size.

{
  "x": 8,
  "y": 158.8125,
  "width": 544,
  "height": 340,
  "top": 158.8125,
  "right": 552,
  "bottom": 498.8125,
  "left": 8
}

All sorts of useful info!

So Why Not Just Uuse componentDidMount?

Yes, that works too. But it's less elegant because you have to save the ref first. The refCallback API calls your function with a nice reference already packaged in.

However, you might still have to do that if your component size changes after initial render. Observe:

Clicking the shuffle button doesn't report new sizing information up the hierarchy. That's not good...

If your component changes size without re-mounting, you have to re-measure its size in componentDidUpdate as well. But that way lies trouble... you can fall into the infinite recursion trap.

You can solve the problem with a lock, like this: 

Keep clicking shuffle and sizing info is always correct.

The key is enabling size reporting when you logically know size is going to change, in shuffle, and disabling it as soon as you report the change in componentDidUpdate.

shuffle = () => {
    this.doReportSize = true;
    this.setState({
      text: faker.lorem.paragraphs(Math.random() * 10)
    });
  };

  refCallback = element => {
    if (element) {
      this.elementRef = element;
      this.props.getSize(element.getBoundingClientRect());
    }
  };

  componentDidUpdate() {
    if (this.doReportSize) {
      this.props.getSize(this.elementRef.getBoundingClientRect());
      this.doReportSize = false;
    }
  }

Oof, not pretty. Setting a this.elementRef in our callback, messing around with class properties for flags. Pretty sure we could've just used the new React.createRef() API in combination with componentDidMount and componentDidUpdate.

Would still need the flag to prevent infinite loops, however.

I wonder if using class properties to make render flags like that will continue to work when asynchronous rendering comes with React 17...

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
web dev ,react.js ,react component

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}