Lesson 8 of 15

Ref and Layout Coordination

Explains how refs and layout effects provide a safe imperative bridge in React, enabling direct DOM access, layout measurement, and precise timing without breaking render purity or concurrent guarantees.

Context / Motivation

So far, React has taught us to think declaratively — describe what the UI should look like, not how to manipulate it.

But the real world sometimes requires breaking that rule. Some interactions can’t be purely declarative:

  • Measuring an element’s size,
  • Managing focus,
  • Controlling a media player,
  • Integrating with third-party libraries (charts, maps, animations).

These operations require direct access to the DOM, and they must be done at the right time — after React finishes rendering.

React solves this through the concept of refs (references) and layout effects, which together provide a safe, predictable way to coordinate imperative actions with React’s declarative world.


The Problem: How Do You Touch the DOM Safely?

Imagine you want to focus an input when the component mounts.

A naive attempt might look like this:

function Login() {
  return <input id="username" />;
}
 
document.getElementById("username").focus(); // ❌ too early

But this runs before React has actually committed the DOM. If React hasn’t finished rendering, this call fails or targets an outdated element.

You can’t safely manipulate DOM nodes from outside React — it controls the lifecycle now.

So React provides refs to let you reach into its DOM tree safely.


Refs: A Way to Hold Onto Mutable Values

A ref is an object that persists across renders and holds a .current property.

You create one using useRef:

const ref = React.useRef();

You can attach it to a DOM element via the ref prop:

function Login() {
  const inputRef = React.useRef();
 
  React.useEffect(() => {
    inputRef.current.focus(); // ✅ runs after DOM is ready
  }, []);
 
  return <input ref={inputRef} />;
}

Now, inputRef.current points to the actual <input> element after React commits. You can read or modify it — but you must do so in an effect, never in render.


When Refs Are Useful

Use CaseExample
Managing focusinputRef.current.focus()
Triggering animationelementRef.current.animate(...)
Measuring layoutelementRef.current.getBoundingClientRect()
Storing mutable data that doesn’t trigger re-rendere.g., previousCount.current = count
Integrating with non-React librariesD3, Mapbox, etc.

Mutable vs. Reactive

State (useState) triggers re-renders when changed. Ref (useRef) does not — it’s just a container that persists across renders.

const count = React.useRef(0);
count.current += 1; // changes value, but no re-render

You use refs when you want to store data across renders without triggering updates. They’re like instance fields in class components.


Layout Coordination: useLayoutEffect

Now, sometimes you need to run code before the browser paints the screen — for example, to measure an element’s size before an animation or to scroll to a position.

If you use useEffect, it runs after paint, which can cause flicker:

function Box() {
  const boxRef = React.useRef();
 
  React.useEffect(() => {
    const rect = boxRef.current.getBoundingClientRect();
    console.log(rect.height); // runs after paint
  });
 
  return <div ref={boxRef} style={{ height: 200 }} />;
}

If the DOM changed size or layout between renders, the user might briefly see the wrong layout before your measurement or scroll adjustment happens.

React provides useLayoutEffect for these cases.

function Box() {
  const boxRef = React.useRef();
 
  React.useLayoutEffect(() => {
    const rect = boxRef.current.getBoundingClientRect();
    console.log(rect.height); // runs before paint
  });
 
  return <div ref={boxRef} style={{ height: 200 }} />;
}

useLayoutEffect runs synchronously after the DOM is mutated but before the browser paints. That means the user never sees an intermediate layout.


Render → Commit → Paint: Where Effects Fit

PhaseWhat HappensExample
RenderReact runs your component, builds new virtual DOM (pure phase)No refs yet
CommitReact updates the real DOMRef .current is now valid
Layout EffectsRun immediately after DOM updates, before paintuseLayoutEffect
PaintBrowser draws new UI on screenUser sees it
Passive EffectsRun after paintuseEffect

So:

  • Use useLayoutEffect when your effect reads or writes layout.
  • Use useEffect when your effect synchronizes external systems (like data fetching, logging, or subscriptions).

Example: Avoiding Flicker with useLayoutEffect

Suppose you want to scroll an element into view when a new message arrives.

function Chat({ messages }) {
  const endRef = React.useRef();
 
  React.useEffect(() => {
    endRef.current.scrollIntoView(); // ❌ may cause visible jump
  }, [messages]);
 
  return (
    <div>
      {messages.map((m) => (
        <p key={m.id}>{m.text}</p>
      ))}
      <div ref={endRef} />
    </div>
  );
}

This scrolls, but sometimes users see a flicker as the layout jumps after paint.

Switch to useLayoutEffect:

React.useLayoutEffect(() => {
  endRef.current.scrollIntoView(); // ✅ before paint, no flicker
}, [messages]);

The scroll now happens before the browser paints, so the user never sees the intermediate layout.


Refs Beyond DOM: Storing Mutable Values

Refs are not only for DOM nodes. You can use them to store any mutable value that persists between renders.

For example, to store the previous state:

function Counter() {
  const [count, setCount] = React.useState(0);
  const prevCount = React.useRef();
 
  React.useEffect(() => {
    prevCount.current = count;
  });
 
  console.log("Prev:", prevCount.current, "Current:", count);
  return <button onClick={() => setCount((c) => c + 1)}>+</button>;
}

This pattern doesn’t trigger a re-render, but it lets you remember “previous” values — useful for diffing or comparison logic.


Common Pitfall: Reading Refs During Render

Avoid reading ref.current in the render phase. During render, React might still be working with the previous DOM tree, and ref.current may not yet point to the committed element.

Example of unsafe read:

function Box() {
  const ref = React.useRef();
  console.log(ref.current.offsetHeight); // ❌ may be null
  return <div ref={ref} />;
}

Instead, always read or write refs in effects, not in render.


Evolution / Refinement

  • Before Hooks, refs existed as React.createRef() and were mostly used in class components.
  • With Hooks, useRef unified instance fields and DOM access.
  • The addition of useLayoutEffect gave precise timing control for layout-sensitive work.
  • Together, they form the imperative bridge between React’s declarative rendering and the real world’s mutable systems.

Principle / Takeaway

Refs are React’s way of remembering mutable objects across renders. > Layout effects are how you safely coordinate reads and writes to the DOM.

In short:

useRef → persistent mutable value
useEffect → after paint (side effects)
useLayoutEffect → before paint (layout coordination)

By separating these responsibilities, React keeps declarative code pure while still giving you precise control when imperative work is unavoidable.


Next, we’ll move to Lesson 9 — Context and Prop Drilling, where we’ll look at how React solves the problem of passing data through many layers without breaking its one-way data flow model.