Sub topic 3
Reading React Profiler Results
Assume you can write function components, use useState, pass props, and run a React app in development mode. The Profiler helps explain slow or unexpected renders after the Components tab shows which values changed.
Reading the flamegraph and ranked charts
Record a Profiler session while reproducing the problem. The flamegraph shows the component tree for a selected commit, which is one completed screen update. Wider or darker items usually took more time to render. A parent can appear expensive because its children rendered inside it, so inspect both levels.
The ranked chart sorts components by render time. Use it to find the largest individual costs. Switch between commits to see whether the cost happens once or repeatedly.
| View | Best question |
|---|---|
| Flamegraph | Which part of the tree rendered during this update? |
| Ranked chart | Which component consumed the most render time? |
Finding out why a component rendered
Select a component in the Profiler and inspect its render reason when that feature is available. Common reasons include changed props, changed state, changed context, or a parent render.
A prop can look unchanged in the UI but still be a new object or function. React compares memoized props by reference, so two separately created objects are different even when their contents match.
Fixing unnecessary renders with memo and useCallback
memo skips a child render when its props are equal. useCallback keeps a function reference stable between renders. Use them when profiling shows that changing references cause repeated, unnecessary child work.
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 (
<>
<p>Selected: {selected ?? "none"}</p>
<ul>{items.map((label) => <Item key={label} label={label} onSelect={handleSelect} />)}</ul>
</>
);
}Standalone DevTools
Standalone React DevTools can connect to React Native applications and some embedded React views. The available tabs and profiling support depend on the platform and integration. Confirm that the app runs a development or profiling-enabled build before diagnosing performance.
Putting it together: a debugging workflow
- Reproduce the issue and record a short Profiler session.
- Use the ranked chart to choose a costly or frequently rendered component.
- Inspect its render reason and changed props.
- Stabilize only the prop or handler that causes unnecessary work.
- Record the same interaction again and compare the charts.
Exercise
Why might a memoized Item still render when its label has not changed?
Show answer
Its onSelect prop may be a new function on every parent render. Use useCallback when profiling confirms that this reference change causes unnecessary work.