React Foundations · Performance

Measuring renders with Profiler

Profiler

Measuring renders with <Profiler>

Before you optimise, you need to know what is actually re-rendering. React ships a built-in component for exactly that: <Profiler>. Wrap any part of your tree in it and pass an onRender callback:

<Profiler id="list" onRender={(id, phase) => console.log(id, phase)}>
  <List />
</Profiler>

React calls onRender after it commits a render of the wrapped tree. The callback receives the id you gave the Profiler, the phase ('mount' or 'update'), and timing data such as actualDuration — how long the commit took.

Because onRender fires on every commit, you can count renders: keep a ref, increment it in the callback, and render the value. That count is a simple, deterministic signal that a subtree re-rendered — the first step before reaching for useMemo, useCallback, or memo.

onRender runs after the commit, so a value it writes shows up on the next render. Profiling is a development aid: it measures, it does not change what your components render.

Your job is to wrap a subtree in <Profiler> and report how many renders it has performed.

Challenge

You want to see how many times a subtree renders, and nothing on the page reports it. Export an App component from App.jsx that wraps a <button> in <Profiler> and counts the renders onRender reports.

Keep the count in a ref, increment it inside onRender, and show it in a <p> as Renders: {count}. The button increments a count state on click.

The Test clicks the button once and expects the paragraph to read Renders: 1. The starter shows a static Renders: 0 and never wraps anything in <Profiler>, so the Test fails.

App.jsx
export function App() {
  return (
    <>
      <button>Count: 0</button>
      <p>Renders: 0</p>
    </>
  );
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.