PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteTo 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.
#1 Best Overall
| 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.
Rank #3
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.
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.
Quick Recap
Best Value
Rank #4
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.currentdoes 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.




