Sub topic 8
Highlighting updates to spot extra renders
This chapter assumes you can write function components, use useState and props, and open the Components tab in React Developer Tools. You should also be able to run a React app locally in development mode. Here you will turn on update highlighting, read what it shows, and use it to find components that render more often than they need to.
What highlighting shows
When a component renders, React calls its function again. Most of the time this is cheap, but a render you did not expect is often the first sign of a performance problem or a logic bug. Highlighting makes renders visible without any code changes.
With highlighting on, React Developer Tools draws a colored outline around the DOM nodes of each component that rendered. The outline appears briefly and then fades. Components that render often get a more intense color.
You can use this to answer one question quickly: when I do X, which parts of the screen render? If the answer is "much more than the part that changed," you have found something to investigate.
Turning it on
- Open the browser developer tools on your running app and select the Components tab.
- Click the gear icon in the Components tab toolbar to open settings.
- In the General section, enable "Highlight updates when components render."
- Interact with the page and watch the outlines.
To stop, clear the same checkbox. Highlighting adds drawing work on every render, so turn it off when you measure timing in the Profiler.
The exact location of this setting has moved between extension versions. If you cannot find it, check the General settings page of the Components tab first.
A sample app to try it on
Use the list from the Topic Spine. Each item is wrapped in memo, yet every item re-renders when you select one.
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>
);
}Render <List items={['Apple', 'Banana', 'Cherry', 'Date']} />, turn on highlighting, and click "Banana."
What you should expect to see:
- The
<p>showing the selection updates. This render is needed, becauseselectedchanged. - Every
<li>also flashes. This is the extra work. Only the text in the paragraph changed, and the item labels did not.
Why the items flash
List owns the selected state. Clicking an item calls setSelected, so List renders again. A parent render renders its children by default, and memo is meant to stop that when the props are unchanged.
memo compares props with Object.is. The label strings are equal between renders. The onSelect prop is not: (value) => setSelected(value) is created inside List, so each render produces a new function object. Object.is(oldFn, newFn) is false, memo sees a changed prop, and Item renders.
Highlighting tells you that this happens. It does not tell you why. The Profiler's render reasons, covered in a later chapter, name the changed prop. For now, treat the flashing items as a lead.
Reading highlight patterns
Different patterns point to different causes. The table lists the ones you will meet most often.
| Pattern you see | Likely cause | What to check |
|---|---|---|
| Everything on the page flashes after a small interaction | State or context lives too high in the tree | Move state closer to where it is used |
| Siblings of the changed element flash | Parent re-rendered and children are not memoized, or their props changed identity | Props that are inline objects, arrays, or functions |
| Items flash while you type in an unrelated input | Input state is in a shared ancestor | Local state for the input, or splitting the component |
| Flashing continues with no interaction | An effect sets state in a loop, a timer, or a subscription | The dependency arrays of effects, and the interval or subscription callbacks |
| Only the expected element flashes | Rendering is scoped correctly | Nothing to fix |
The "continuous flashing" row deserves attention. If the outlines pulse while you are not touching the page, something is scheduling renders. That is not always a bug, because clocks and live data update on purpose. It is a bug when the updates do not change what the user sees.
Highlighting versus the Profiler
Both tools show renders, but they answer different questions.
| Question | Highlighting | Profiler |
|---|---|---|
| Did anything render when I did this? | Yes, at a glance | Yes, but you must record and open the commit |
| How long did the render take? | No | Yes |
| Why did a component render? | No | Yes, with render reasons enabled |
| Is it quick to try? | One checkbox | Needs a recording session |
Use highlighting for fast triage. Move to the Profiler when you need numbers or reasons.
Edge cases and limits
- Development builds double-render. In development with
StrictMode, React renders components twice to find impure code. Highlighting reflects commits to the DOM, so you should still see one flash per update. Do not count console logs inside the component as evidence of the number of commits. - A render is not always a DOM change. A flash means the component function ran and React committed. The DOM may have received no actual change. React skips DOM writes when the output is identical. A flash is therefore a signal about work in JavaScript, not about layout cost.
- Fast renders are cheap. Twenty list items re-rendering for a trivial
<li>costs little. Do not addmemoeverywhere because things flash. Fix a case when the render is expensive, when the list is large, or when the flashing reveals a logic error. - Colors indicate frequency. The outline color shifts as the same component renders repeatedly in a short time. A deep color on one component during a drag or a keystroke means it renders on every event.
- Production builds do not support it. Highlighting relies on development hooks. If it does nothing, check that you are not running a minified production build. Build modes get their own chapter.
Fixing the example
Once you know the cause is the unstable onSelect, give Item a stable function. useCallback returns the same function object across renders as long as its dependencies do not change. The state setter from useState is already stable, so the dependency array can be empty.
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>
);
}Reload, keep highlighting on, and click "Banana." Now the paragraph flashes, and the items do not flash when the label is unchanged. Click "Banana" again: selected is already 'Banana', and React bails out of the update, so nothing flashes at all.
A full discussion of memoization and its costs comes later in the tutorial.
Verifying a fix
Highlighting is useful as a before-and-after check. Follow the same routine each time.
- Reproduce the interaction with highlighting on and note which regions flash.
- Make one change.
- Reload to get a clean state, repeat the exact same interaction, and compare.
- If the flashing did not shrink, undo the change. A fix that does not change the pattern is not the fix.
Common mistakes
- Treating every flash as a problem. Many renders are correct and cheap.
- Wrapping components in
memowithout checking props. Inline objects, arrays, and functions defeat it, as the first example shows. - Using
useCallbackon a handler passed to a component that is not memoized. The stable function has no effect, because the child renders with its parent anyway. - Testing in a production build and concluding that the feature is broken.
- Changing several things at once, so you cannot tell which one removed the flashing.
Exercise
In the first version of the list, Item is wrapped in memo and its label prop does not change, yet all items flash when you click one. Name the prop that causes it and explain why memo does not help.
Show answer
The onSelect prop. The arrow function (value) => setSelected(value) is created during each render of List, so each render passes a new function object. memo compares props with Object.is, which treats the new function as different from the old one, so Item renders again even though label is unchanged.
Exercise
You turn on highlighting and see the outlines pulse every second while you are not touching the page. List two likely causes and one way to confirm which it is.
Show answer
Likely causes are a setInterval or subscription callback that sets state, and an effect that sets state on every run, for example because of a missing or wrong dependency array. To confirm, open the component's hooks in the Components tab and watch which state value changes, or search the code for timers and effects near the pulsing region. If the value set is identical to the old one, the update is unnecessary and can be removed.