← Back to React, page 7

react-0130 · Medium mid

How do you use React DevTools and the Profiler to debug state and find slow renders?

The Components tab shows the component tree with props, state, hooks and context that you can inspect and edit live, and highlights what renders. The Profiler tab records interactions and shows a flame chart and a ranked chart of render times, plus why each component rendered. Enable "Highlight updates when components render" and "Record why each component rendered". Profile production-like builds, because development is slower. The Profiler component and useDebugValue add code-level hooks.

Full answer

React DevTools is a browser extension (and standalone app) that understands React's internal tree.

Components tab. It lists components as they are nested, not the DOM. Selecting one shows its props, state, hooks (in order, with custom hook names), context values and the source location. You can edit props, state and hook values live to test cases, jump to the DOM node, and find components by name. The "Highlight updates when components render" setting flashes borders around components when they re-render, which quickly reveals unnecessary updates. Settings also allow filtering components (hide host elements or library internals) and showing Strict Mode double render logs.

Profiler tab. Start recording, perform an interaction, stop. The flame chart shows each commit: how long each component took to render, with gray components not rendered in that commit. The ranked chart sorts components by render time. Turn on "Record why each component rendered while profiling" to see reasons such as "props changed (name)", "hooks changed", "parent rendered". Commits can be stepped through. Timings are only meaningful in a production-like build, because development builds are slower. React provides a profiling build for production measurements.

Code-level tools:

  • <Profiler id onRender> wraps a subtree and calls back with the phase (mount, update, nested-update), actual and base durations, and commit time. It works in development and profiling builds and is disabled in a normal production build.
  • useDebugValue(value) adds a label to a custom hook in DevTools.
  • displayName improves names of anonymous or wrapped components (memo, forwardRef).
  • The Performance panel of the browser shows the same renders in the context of long tasks and paint, which is where INP issues become visible.

Workflow: reproduce the slow interaction, profile, find the components that render too often or take longest, read why, apply the fix (move state, memo, split, virtualize), profile again and compare.

The example uses the Profiler component and useDebugValue.

jsx
import { Profiler, useDebugValue, useState } from 'react';

function useCounter() {
  const [count, setCount] = useState(0);
  useDebugValue(count > 0 ? 'active' : 'idle');
  return [count, setCount];
}

function Counter() {
  const [count, setCount] = useCounter();
  return <button onClick={() => setCount(count + 1)}>Add ({count})</button>;
}
Counter.displayName = 'Counter';

function report(id, phase, actualDuration) {
  console.log('Profiler ' + id + ': phase=' + phase + ', duration is a number=' + (typeof actualDuration === 'number'));
}

export default function App() {
  return (
    <Profiler id="counter-area" onRender={report}>
      <Counter />
    </Profiler>
  );
}

The Profiler callback fired for the first render (mount) and for the click (update). The DevTools panels cannot be run in this environment, so the tab descriptions come from the documentation and not from a run here. Output of the run: Profiler counter-area: phase=mount, duration is a number=true Profiler counter-area: phase=update, duration is a number=true

Key points

  • The Components tab inspects and edits props, state, hooks and context, and can highlight re-renders.
  • The Profiler records commits with flame and ranked charts, and can show why components rendered.
  • Profile production-like builds for meaningful timings.
  • The Profiler component, useDebugValue and displayName provide code-level hooks.
  • Workflow: reproduce, profile, read the reason, fix, profile again.

Common mistakes

  • Measuring performance in the development build.
  • Optimizing without checking why a component rendered.
  • Leaving Profiler wrappers in production code and expecting data (they are disabled in normal production builds).
  • Ignoring unnamed components, which make the tree hard to read.
  • Reading the flame chart without using the ranked view for the worst offenders.

Follow-ups

  • What does "why did this render" tell you, and what are the common reasons?
  • How do you profile a production build?
  • How does the Performance panel complement the React Profiler?

Tips

  • Mention both tabs and one setting for each.
  • Say you profile production-like builds.
  • Name the Profiler component as the programmatic option.