Sub topic 15
Putting it together: a debugging workflow
This chapter assumes you can write function components, use useState and props, and have used the browser's Elements and Console panels. It also assumes you have met the Components and Profiler tabs covered earlier in this tutorial. Here you combine them into one repeatable routine.
Two kinds of bugs, two starting tabs
Most React problems fall into one of two groups. Pick the tab that matches the symptom.
| Symptom | Start in | Look for |
|---|---|---|
| Wrong text, missing item, stale value | Components | Props, state, and hook values that differ from what you expect |
| Slow typing, laggy clicks, janky lists | Profiler | Components that render often or take a long time |
The workflow
- Reproduce the bug in a development build. Development builds give readable component names and render reasons. Use a profiling build only when you need production-like timings.
- Locate the component. Select it in the page with the inspect arrow, or use the search box in the Components tab.
- Read its props, state, and hooks. Decide whether the bad data came in as a prop or was produced inside the component.
- Follow the data upward. If a prop is wrong, select the parent and check what it passed down. Repeat until you find the component that first holds the wrong value.
- Test a fix without editing code. Change the value in the sidebar and check whether the UI is correct. This confirms your theory before you touch the source.
- Switch to the Profiler for performance problems. Turn on "Record why each component rendered while profiling" in the Profiler settings, record the slow interaction, and stop.
- Find the expensive or repeated renders. Use the ranked chart to find the slowest components, then check the render reason for each one.
- Apply the smallest fix and record again. Compare the new recording with the old one.
A worked example
A list is slow to respond when you select an item. You record a click in the Profiler. The ranked chart shows every Item rendering, even though only selected changed. The render reason for Item says that its props changed, and the changed prop is onSelect.
The cause is the inline function. It is a new function on every render of List, so memo sees a different prop each time.
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>
);
}Record the same click again. Items whose label has not changed should now be skipped, and the Profiler shows them as not rendered.
Common mistakes
- Profiling a production build. Names are shortened and render reasons are unavailable. Use development, or a profiling build for timings.
- Trusting one recording. Timings vary between runs. Record the interaction a few times before drawing conclusions.
- Fixing the child when the parent is at fault. A wrong prop is created higher up the tree. Follow it up before changing the child.
- Forgetting to re-measure. A fix is not confirmed until a new recording shows the change.
Exercise
In the list example, the Profiler says Item re-rendered because its props changed, and the changed prop is onSelect. You wrap the handler in useCallback with an empty dependency array. Which tab and setting do you use to confirm that the fix worked, and what do you expect to see?
Show answer
Use the Profiler tab. Record the same click again, with "Record why each component rendered while profiling" still enabled. Items whose label did not change should no longer appear as rendered, and only the updated components should show a render reason.
Exercise
A product card shows the wrong price. Which tab do you start in, and what is your first check?
Show answer
Start in the Components tab. Select the card and read its props. If the price prop is already wrong, select the parent and check where the value comes from. If the prop is right, check the card's state and hooks.