← Back to React, page 6

react-0105 · Easy fresher

What causes a React component to re-render, and what does not?

A component re-renders when its own state changes, when its parent re-renders (even with the same props, unless memoized), or when a context it reads changes. It does not re-render when you mutate a ref, assign to a plain variable, or set state to the same value (React bails out). Re-rendering means React calls the function again. It does not always mean the DOM changes.

Full answer

A render is a call to your component function to compute what the UI should look like. React then compares the result with the previous one and updates the DOM only where it differs. So "re-render" and "DOM update" are different things.

Triggers:

  1. State change. Calling a state setter with a new value schedules a render of that component.
  2. Parent render. When a component renders, React renders its children too, with new props objects, even if the values are equal. This is the most common cause of extra renders. React.memo can skip a child when its props are shallowly equal.
  3. Context change. Every component that reads a context with useContext re-renders when the provider's value changes by reference, even if its parent did not render.
  4. Hook subscriptions, such as useSyncExternalStore or a store selector, when their data changes.

Not triggers:

  • Mutating a ref (ref.current = 1) or a normal variable. React does not know about it.
  • Setting state to the same value (compared with Object.is). React bails out, although it may call the component one more time before stopping in some cases, without updating children.
  • Mutating state objects directly (user.name = 'x'). The reference did not change, so React may not notice.

Props changes alone never cause a render, because props only change when the parent renders.

Why this matters. Slow screens usually come from too many unnecessary renders of expensive components. The first tools are to move state down, split components, and then use memo, useMemo and the Compiler. Do not try to block all renders. Renders are cheap when components are light.

The example logs which components render for each type of change.

jsx
import { createContext, memo, useContext, useRef, useState } from 'react';

const ThemeContext = createContext('light');

function Plain() {
  console.log('  Plain child rendered');
  return <p>plain child</p>;
}

const Memoized = memo(function Memoized() {
  console.log('  Memoized child rendered');
  return <p>memoized child</p>;
});

function ThemeReader() {
  const theme = useContext(ThemeContext);
  console.log('  ThemeReader rendered with ' + theme);
  return <p>theme: {theme}</p>;
}

export default function App() {
  const [count, setCount] = useState(0);
  const [theme, setTheme] = useState('light');
  const clicks = useRef(0);

  console.log('App rendered');

  return (
    <ThemeContext value={theme}>
      <button onClick={() => (clicks.current += 1)}>Change ref</button>
      <button onClick={() => setCount(0)}>Set same value</button>
      <button onClick={() => setCount(count + 1)}>Set new value</button>
      <button onClick={() => setTheme('dark')}>Set theme</button>
      <Plain />
      <Memoized />
      <ThemeReader />
    </ThemeContext>
  );
}

The check pressed the four buttons in order: change ref, set the same value, set a new value, and change the theme. The first two presses printed nothing, so no render happened. Set new value rendered App and Plain, but not Memoized. ThemeReader rendered each time only because its parent did, so this example does not isolate a context-only render. Output of the run: App rendered Plain child rendered Memoized child rendered ThemeReader rendered with light App rendered Plain child rendered ThemeReader rendered with light App rendered Plain child rendered ThemeReader rendered with dark

Key points

  • State changes, parent renders and context changes trigger renders.
  • A render is a function call. The DOM only changes where output differs.
  • Children re-render with their parent unless memoized.
  • Ref mutations, variable assignment and same-value state do not trigger renders.
  • Fix slowness by moving state down before memoizing.

Common mistakes

  • Assuming a child only re-renders when its own props change.
  • Thinking a re-render always updates the DOM.
  • Mutating state and expecting a re-render.
  • Wrapping everything in memo before measuring.
  • Forgetting that a context change re-renders all readers.

Follow-ups

  • How does React.memo compare props?
  • How can you prevent context readers from re-rendering on every change?
  • What does it mean that React "bails out" of a state update?

Tips

  • Separate "render" from "DOM update". Interviewers listen for it.
  • List the three triggers and the common non-triggers.
  • Suggest moving state down as the first fix.