October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Get Component Height in React

Attach a ref to the rendered DOM node and measure it after React commits. Choose among bounding-rectangle, layout, inner-box, and scroll-height measurements based on what you need.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To get a component’s rendered height in React, attach a ref to the DOM element you want to measure and read its height after React has committed the element. Use getBoundingClientRect().height for its rendered border-box size. If later content or layout changes can alter the height, track those changes with ResizeObserver rather than relying on a one-time measurement.

Measure a DOM element after it renders

A React component has no measurable height until it produces DOM. A ref gives you access to the rendered element; React fills the ref during the commit, so it is null during the initial render. Read it in an effect or an event handler, not in the component’s render body. See React’s guide to manipulating the DOM with refs.

import { useLayoutEffect, useRef, useState } from 'react';

function Panel() {
  const panelRef = useRef(null);
  const [height, setHeight] = useState(0);

  useLayoutEffect(() => {
    const node = panelRef.current;
    if (node) {
      setHeight(node.getBoundingClientRect().height);
    }
  }, []);

  return (
    <section ref={panelRef}>
      <p>Measured height: {height}px</p>
    </section>
  );
}

This measures the section after the initial commit. The measurement is stored in state because the displayed number is part of the JSX. Assigning ref.current does not trigger a render; use state when the measurement needs to update rendered output. If you only need the number for an imperative operation, you may not need state. See React’s useRef reference.

Choose the height measurement that matches your goal

Browser measurement APIs describe different boxes. Select one based on whether you need rendered geometry, layout dimensions, the inner box, or the complete content extent.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
API What it measures Important distinctions
getBoundingClientRect().height The rendered bounding-box height, including padding and borders. May be fractional and reflects transforms. See MDN’s getBoundingClientRect() reference.
offsetHeight An integer layout height including padding, borders, and a rendered horizontal scrollbar. Excludes margins; returns zero if the element or an ancestor is display: none. See MDN’s offsetHeight reference.
clientHeight The inner height including content and padding. Excludes borders, margins, and the scrollbar.
scrollHeight The full content height, including content not currently visible because it overflows. Useful when you need the full scrollable extent rather than the visible box.

MDN’s dimension guide explains the relationship among these measurements. Margins are not included in getBoundingClientRect() or offsetHeight. Because transforms affect the rendered rectangle but not the integer layout measurement, the two can differ.

Choose when the measurement runs

Use useLayoutEffect when the first paint must use the measurement

useLayoutEffect runs after the DOM is updated but before the browser repaints. It is appropriate when the measured value must affect the first visible layout—for example, positioning an element before it appears. React documents layout measurement as a use for this hook in its built-in Hooks reference. Keep work in layout effects narrow, since it runs before repaint.

Use useEffect when an update after paint is acceptable

If a brief initial render with a default value is acceptable, a normal useEffect can perform the measurement. The effect in the example runs once after the initial commit; it does not keep the height current when the element later changes size.

Track later size changes

A one-time measurement can become stale when text changes, a font loads, or responsive layout changes. To keep the value current, observe the element with the browser’s ResizeObserver API and disconnect the observer during effect cleanup.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { useEffect, useRef, useState } from 'react';

function Panel() {
  const panelRef = useRef(null);
  const [height, setHeight] = useState(0);

  useEffect(() => {
    const node = panelRef.current;
    if (!node) return;

    const observer = new ResizeObserver(() => {
      setHeight(node.getBoundingClientRect().height);
    });

    observer.observe(node);
    setHeight(node.getBoundingClientRect().height);

    return () => observer.disconnect();
  }, []);

  return (
    <section ref={panelRef}>
      <p>Current height: {height}px</p>
    </section>
  );
}

This example reads the same rendered-box measurement on each observation. If the code must support a particular set of browsers, check their ResizeObserver support before relying on the API.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Measure an element inside a child component

A ref on a built-in element such as <div> points to its DOM node. A ref on a custom component does not automatically expose the child’s internal DOM. The child must deliberately pass the ref to the relevant element or expose a specific imperative handle. Current React documentation accepts ref as a prop; React 18 and earlier use forwardRef for the ref-forwarding pattern. Check the project’s React major version before choosing the child-component syntax. React’s references cover useImperativeHandle and ref troubleshooting.

Common measurement mistakes

  • Reading during render: the DOM node may not exist yet, and later renders may not have committed the pending DOM update. Read in an effect or event handler.
  • Expecting a ref update to refresh JSX: changing ref.current does not re-render the component. Put the measured value in state if the UI depends on it.
  • Using a hidden element’s offsetHeight: it is zero when the element or an ancestor has display: none.
  • Confusing the box measurements: margins are excluded, while borders, scrollbars, transforms, and overflowing content are treated differently by each API.
  • Assuming a custom component exposes its DOM: the component must explicitly make the relevant node available to its parent.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.