ReactEasy

What is a re-render?

A re-render is React calling a component function again after the first render, to compute its UI for the current props, state and context. A re-render does not necessarily change the DOM: React compares the new output with the previous one and only commits differences.

Re-render vs DOM update

  • Render: React runs your component code and produces new elements.
  • Commit: React updates the DOM only where the output changed. A component can re-render many times without any visible change, which is why most re-renders are cheap.

Why re-renders exist

They are how React keeps UI in sync with state. Every state change re-renders the component that owns it and, by default, all its descendants.

When they become a problem

  • The component does expensive work during render.
  • A large subtree re-renders on every keystroke or scroll event.
  • Effects run on every render because dependencies are unstable. Symptoms: input lag, janky animations, poor INP scores.

Controlling them

  • Keep state close to where it is used.
  • Pass static subtrees as children.
  • Memoize with memo, useMemo, useCallback or let the React Compiler do it.
  • Use transitions for expensive updates.
  • Split context, or use stores with selectors.

Observing

React DevTools "Highlight updates when components render" and the Profiler's "Why did this render?" show what re-rendered and why.

Code example

tsx
1import { useState } from "react";
2
3function Clock() {
4 console.log("Clock rendered");
5 return <p>Static text</p>; // re-renders with the parent, but the DOM never changes
6}
7
8export function Parent() {
9 const [count, setCount] = useState(0);
10 console.log("Parent rendered", count);
11
12 return (
13 <div>
14 <button onClick={() => setCount((c) => c + 1)}>Count {count}</button>
15 {/* Each click: Parent and Clock re-render, but only the button text node is updated in the DOM */}
16 <Clock />
17 </div>
18 );
19}
20
21// Setting the same value again bails out (no re-render of children):
22// setCount(count) when count is unchanged

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app