Sub topic 15
Putting it together: a debugging workflow
The earlier chapters covered each part of React Developer Tools separately. This chapter joins them into one repeatable process. It assumes you can read the Components tab, edit values live, switch on update highlighting, and record a Profiler session. It also assumes you know useState, props, and memo.
Start by classifying the symptom
Most React bugs fall into two groups. Each group has its own first tool, so decide which one you have before you open anything.
| Symptom | Likely cause | First tool |
|---|---|---|
| A screen shows wrong or stale data | A wrong prop, state value, or hook value | Components tab |
| A screen is slow or janky | Too many renders, or renders that cost too much | Highlight updates, then Profiler |
| A value is right in one place and wrong in another | Data changes somewhere between parent and child | Components tab, walking up the tree |
| Typing or clicking lags | A parent re-renders a large subtree | Profiler with render reasons |
The two groups overlap. A component that renders too often can also show stale values. Start with the symptom the user sees, and let what you find send you to the other tab.
Step 1: Reproduce in a build that helps you
Run the app in development mode. Development builds give you readable component names, hook names, and warnings. A plain production build works with the Components tab, but names are often minified and the Profiler is unavailable. If you must profile something production-like, use a profiling build, as covered in the chapter on build modes. Do not draw conclusions about speed from development timings, because development mode adds extra checks and runs slower.
Write down the exact steps that trigger the bug. You will repeat them several times, and a short, reliable sequence saves time in every later step.
Step 2: Wrong data, trace it through the tree
Suppose a product card shows the wrong price. Work as follows.
- Open the Components tab and select the card. You can search by name or use the picker (the arrow icon) and click the element on the page.
- Read the right-hand pane: props, then state, then hooks. Decide whether the wrong value is a prop, a state value, or something derived from a hook.
- If it is a prop, select the parent and look for where it passes that value down. Keep walking up until you find the component where the value first becomes wrong.
- If it is state, check whether the update that should have changed it ran. Trigger the action and watch the value in the pane. It updates as you interact.
The place where the value is first wrong is your suspect. The bug is in that component, or in the code that feeds it. This approach is faster than adding console.log calls everywhere, because you look at real values in the live tree.
Confirm the cause by editing live
Before you change code, test your theory. Double-click the value in the props, state, or hooks pane and enter a new one. If the UI is right once the value is fixed, the fault is in how the value is produced, not in how it is displayed. If the UI is still wrong, your suspect is wrong, and you have saved yourself an edit.
Two limits apply. Edits change only the running app and disappear on reload. Edits to props last only until the parent re-renders and passes the old value again.
Use the Console and source links
With the suspect component selected, switch to the Console. The variable $r holds the selected component.
// In the Components tab, select a component, then switch to the Console:
$r.props; // props of the selected component
$r.type; // the component function or class itself$r.props is useful for copying a large object or calling a function prop by hand. $r.type is the component function, so you can log it and click through to its definition. The pane's source icon takes you to the component's code in the Sources panel, where you can set breakpoints. Use the pane's log icon to print the component's data to the Console as well.
Step 3: Slow or extra renders, find them first
A render that does nothing visible is the most common hidden cost in React. Follow this sequence.
- Open the Components settings and enable the option to highlight updates when components render.
- Perform the slow action once. Each component that renders gets a colored outline. Colors move toward red and orange when a component renders often.
- Ask whether the outlines match what should change. Selecting one list item should outline that item and the selected label. If the whole list flashes, you have extra renders.
Highlighting tells you that something renders too much. It does not tell you why, and it does not give timings. Move on to the Profiler for both.
Record with render reasons on
In the Profiler settings, enable the option to record why each component rendered. Then click record, perform the same action, and stop. Keep the recording short and focused on a single action. A long recording buries the one commit you care about.
Use the commit bar chart at the top to move between commits. Taller, warmer bars are slower commits. Pick the slowest commit that matches your action, and then read it two ways:
- The flamegraph shows the tree structure. Wide bars mean the component and its children took longer. Gray bars did not render in this commit.
- The ranked chart sorts components by their own render time. Use it to find the single most expensive component.
Click a component in either chart. The right-hand pane lists the reason it rendered, such as "Props changed: onSelect". It also shows when "The parent component rendered". The reason decides the fix, as the next section shows.
Step 4: Match the render reason to the fix
| Reason shown | What it means | Typical fix |
|---|---|---|
| Props changed: a function or object | A new reference is created on every parent render | `useCallback` or `useMemo`, or move the value out of the render |
| Props changed: a primitive | The data truly changed | Usually no fix. This render is needed |
| State changed | The component's own state updated | Check that the update was needed. Consider lifting or splitting state |
| Hooks changed | A hook value inside changed | Inspect the hook in the Components tab |
| The parent component rendered | The parent re-rendered and nothing else changed | Wrap the child in `memo` |
The common case is a list whose items are wrapped in memo but still all re-render. In this version the inline onSelect function is new on each render, so memo sees a changed prop every time. The Profiler reports "Props changed: onSelect".
import { memo, useState } from 'react';
const Item = memo(function Item({ label, onSelect }) {
return <li onClick={() => onSelect(label)}>{label}</li>;
});
export function List({ items }) {
const [selected, setSelected] = useState(null);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((label) => (
<Item
key={label}
label={label}
onSelect={(value) => setSelected(value)}
/>
))}
</ul>
</div>
);
}Wrapping the handler in useCallback gives Item the same function on every render. The label prop is a string, so it compares equal when it has not changed, and memo can skip those items.
import { memo, useCallback, useState } from 'react';
const Item = memo(function Item({ label, onSelect }) {
return <li onClick={() => onSelect(label)}>{label}</li>;
});
export function List({ items }) {
const [selected, setSelected] = useState(null);
const handleSelect = useCallback((value) => setSelected(value), []);
return (
<div>
<p>Selected: {selected ?? 'none'}</p>
<ul>
{items.map((label) => (
<Item key={label} label={label} onSelect={handleSelect} />
))}
</ul>
</div>
);
}The empty dependency array is safe here because setSelected is stable, and the callback does not read any other value from the component.
Step 5: Verify with a second recording
A fix is not done until you measure it. Repeat the exact action from Step 1 and record again. After the fix, the unselected items should appear gray in the flamegraph, and the highlight should outline only the changed items. Compare the commit duration with the earlier one. If the numbers did not improve, remove the change, because every memo and useCallback adds code to read and a small comparison cost.
Common mistakes in this workflow
- Profiling in the wrong mode. Development timings overstate the cost of renders. Use them to find which components render, not to judge absolute speed.
- Recording too much. A recording of ten actions makes the reasons hard to match to a cause. Record one action at a time.
- Trusting live edits as fixes. An edit proves a theory. It does not change your source.
- Memoizing everything.
memohelps only when a component renders often with equal props and its render is costly enough to matter. Add it where the Profiler shows a problem. - Forgetting the hidden filters. If a component does not appear in the tree, check the component filters in settings. A filter may be hiding it.
Exercise
A memoized list item shows "Props changed: onSelect" in the Profiler every time any item is clicked, even though the handler logic never changes. What is the cause, what fix would you apply, and how would you confirm that the fix worked?
Show answer
The parent creates a new inline function for onSelect on every render, so memo sees a new prop reference each time. Create the handler once with useCallback (with an appropriate dependency array) and pass that stable function to the item. To confirm, repeat the same click while recording again. The unchanged items should be gray in the flamegraph, the render reason for them should no longer list onSelect, and the commit duration should be lower than in the first recording.
Exercise
A price shows the wrong value in a deeply nested component. Describe the steps you would take in the Components tab to find the source of the bad value, and how you would confirm your theory before editing code.
Show answer
Select the component and read its props, state, and hooks to see whether the wrong value is a prop or local data. If it is a prop, select its parent and check the same value there, and repeat up the tree until you find the component where the value first becomes wrong. To confirm, double-click the value in the pane and enter a correct one. If the UI becomes correct, the fault is in how that value is produced, and you can use the source link to open that component's code.