learn deep. ship safe.Practice testsFollow ByteMunSubscribe

Concept

Rendering

When and why components render, and what triggers a re-render.

Rendering explained

In React, rendering means calling your component functions to work out what the UI should look like. A component re-renders when its state changes, when its parent re-renders, or when a context it reads changes. Rendering itself does not touch the page: React compares the new result with the previous one and then commits only the differences to the DOM.

Render and commit

  1. Trigger: a state update (setState), a parent rendering, or a context value changing.
  2. Render: React calls the components and builds a new tree of elements. This must be pure: same props and state, same output, no side effects.
  3. Commit: React applies the minimal DOM changes, then runs effects.

Re-rendering is normal and usually cheap. It only becomes a problem when a component does heavy work during render or when a large tree re-renders on every keystroke.

Keeping renders fast

  • Keep state close to where it is used, so updates re-render a small part of the tree.
  • React.memo skips re-rendering a component whose props are unchanged; it pairs with useMemo and useCallback to keep props stable.
  • Move expensive calculations out of render, or cache them with useMemo.
  • Use the Profiler in React DevTools to find what actually renders and why, before optimising.

Common mistakes

  • Side effects during render, such as fetching data or changing a variable outside the component.
  • Creating new objects or functions as props every render, which defeats React.memo.
  • Setting state during render without a condition, causing an infinite loop.
  • Optimising everything with memo by default instead of measuring first.

What interviewers ask about React rendering

  • "When does a component re-render?" State change, parent render, or context change.
  • "Does a re-render always update the DOM?" No, only if the output differs.
  • "How would you stop a list re-rendering on every keystroke?" Move the input's state down, memoise the list, keep props stable.
Concept map

What should you learn before Rendering?

  1. Components and compositionBuilding a UI from small reusable pieces and combining them.
  2. PropsInputs passed from a parent component to a child.
  3. StateState changes are what trigger renders.

What to learn after Rendering

Practise Rendering

All topics