Sub topic 8
Highlighting updates to spot extra renders
This chapter assumes you can read a React function component, use useState and props, and open the Components tab described in earlier chapters. Highlighting is the quickest way to see which components render when you interact with your app.
What highlighting shows
When you turn on update highlighting, React Developer Tools draws a colored outline around each component's on-screen output every time that component renders. You don't need to record anything. You click, type, or scroll, and watch which parts of the page flash.
To turn it on:
- Open the Components tab.
- Click the gear icon to open settings.
- Under General, check "Highlight updates when components render."
The outline color shows how often a component renders in a short period. Blue means rarely, and the color moves through green and yellow toward red as renders become more frequent. A component that stays red while you type in an unrelated field is a good suspect.
A worked example
This list memoizes Item, so you might expect only the clicked row to be affected. Run it in development mode.
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>
);
}Click one item. Every row flashes, not just the one you clicked. List renders because its state changed. That creates a new onSelect function for each Item, and memo compares props by identity, so it sees a changed prop and re-renders every row.
Now wrap the handler in useCallback so the prop stays the same between renders.
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>
);
}Click an item again. Only the "Selected" line updates, and the rows stay quiet.
Common mistakes when reading highlights
- Treating a flash as a DOM change. The outline means the component rendered. React may have rendered and then changed nothing in the DOM.
- Assuming every flash is a bug. A parent that owns the state should render when that state changes. Look for components whose props and state did not change.
- Testing a production build. Highlighting needs a development or profiling build, so test your app in development mode.
- Expecting an explanation. Highlighting tells you what rendered, not why. The Profiler's render reasons cover that, and a later chapter explains how to use them.
Exercise
In the first example, you click one row and every row flashes, even though Item is wrapped in memo. What causes this, and how do you confirm the fix with highlighting?
Show answer
The inline arrow function passed as onSelect is a new function on every List render, so memo sees a changed prop and re-renders each Item. Wrap the handler in useCallback with an empty dependency array and pass that stable function. Click a row again with highlighting on. Only the "Selected" text should flash, and the rows should not.