← Back to React, page 6

react-0107 · Easy fresher

Which components re-render when state changes inside one component, and why does passing children as a prop prevent a re-render?

When a component's state changes, React re-renders that component and every child element it creates in its own render. Elements passed in as children were created by the parent earlier, so they keep the same identity and React reuses the previous result without calling those children again. A component that creates its children itself re-renders them each time. This is why moving state into a small wrapper component limits re-renders.

Full answer

React compares elements by identity before rendering them. When a component function runs, it produces new element objects for the children it writes in JSX, and React must render those children again. If an element object is exactly the same object as in the previous render, React skips it, because the element and its props did not change.

Consider two ways to write a counter. In the first, the counter component writes <Plain /> inside its own JSX. Every state change runs the counter function, which creates a new <Plain /> element, so Plain re-renders. In the second, the counter receives children as a prop, and the parent that created <Static /> does not re-render when the counter's own state changes. The children prop keeps the same element object, so Static is skipped.

This gives a free optimization: if only part of a component depends on frequently changing state, move that state and the parts using it into a new component, and pass the rest as children or props. No memo is required.

Remember the limits. If the parent of the counter re-renders, it creates a new <Static /> element, and Static re-renders too unless it is memoized. Passing an inline object or function as a prop makes memo fail for the same identity reason.

Puzzle tip: to predict renders, ask who owns the state, and then ask which elements were created in the render that is running.

The example has both patterns and logs the renders.

jsx
import { useState } from 'react';

function Static() {
  console.log('  Static rendered');
  return <p>static</p>;
}

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

function CounterWithChildren({ children }) {
  const [count, setCount] = useState(0);
  console.log('CounterWithChildren rendered');
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>With children: {count}</button>
      {children}
    </div>
  );
}

function CounterWithOwnChild() {
  const [count, setCount] = useState(0);
  console.log('CounterWithOwnChild rendered');
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Own child: {count}</button>
      <Plain />
    </div>
  );
}

export default function App() {
  return (
    <div>
      <CounterWithChildren>
        <Static />
      </CounterWithChildren>
      <CounterWithOwnChild />
    </div>
  );
}

The check pressed each button once. After the first button only the counter renders, and after the second button the counter and Plain render. Output of the run: CounterWithChildren rendered Static rendered CounterWithOwnChild rendered Plain rendered CounterWithChildren rendered CounterWithOwnChild rendered Plain rendered

Key points

  • A state change re-renders the component and the elements it creates during that render.
  • Elements received as children keep their identity and are skipped.
  • Moving state into a wrapper limits re-renders without memo.
  • A parent re-render creates new elements, so children re-render again unless memoized.
  • To predict renders, find the state owner and who created each element.

Common mistakes

  • Assuming a child re-renders only when its props change.
  • Adding memo when moving state down would be simpler.
  • Forgetting that a parent re-render creates new child elements.
  • Passing inline objects and functions to memoized children.
  • Lifting state higher than needed.

Follow-ups

  • What changes if App itself re-renders?
  • How does React.memo decide to skip a render?
  • How does the React Compiler relate to this identity trick?

Tips

  • Explain element identity in one sentence.
  • Offer "move state down" as the first optimization.
  • Predict the log before running it. Interviewers like that.