Sub topic 3
Navigating the component tree
This chapter assumes you can write React function components, use useState and props, and use the browser's Elements and Console panels. It also assumes you have React Developer Tools installed and a React app running in development mode. The previous chapter covered installation.
What the Components tab shows
The Components tab shows the tree of React components that produced the current page. The browser's Elements panel shows the DOM, which is the output of those components. The two trees often differ a lot. A single <ProductCard /> can render a dozen DOM nodes, and a context provider renders none.
The tab has two panes:
- The left pane is the tree. Each row is a component instance, indented under its parent.
- The right pane shows details for the selected row: props, state, hooks, and where the component was rendered from.
This chapter covers the left pane and how to move around in it. The next chapter covers the right pane in detail.
A sample app to explore
Use this small app to follow along. It has a context provider, a layout that takes children, and a memoized list item.
import { createContext, memo, useContext, useState } from 'react';
const ThemeContext = createContext('light');
const Row = memo(function Row({ name }) {
const theme = useContext(ThemeContext);
return <li className={theme}>{name}</li>;
});
function Layout({ children }) {
return <main>{children}</main>;
}
function Page() {
const [names] = useState(['Ada', 'Grace', 'Linus']);
return (
<ul>
{names.map((name) => (
<Row key={name} name={name} />
))}
</ul>
);
}
export default function App() {
return (
<ThemeContext.Provider value="dark">
<Layout>
<Page />
</Layout>
</ThemeContext.Provider>
);
}With host (DOM) nodes hidden, the tree looks roughly like this:
App
ThemeContext.Provider
Layout
Page
Row key="Ada" [Memo]
Row key="Grace" [Memo]
Row key="Linus" [Memo]The exact rows depend on your filter settings and DevTools version. If you see extra div or ul rows, host nodes are visible. The filters section of a later chapter explains how to toggle them.
Reading a row
Each row in the tree carries a few pieces of information.
| Part of the row | Meaning |
|---|---|
| Name | The component's `displayName`, or its function or class name. |
| `key="..."` | The React key, shown next to the name when one is set. |
| Badge such as `Memo` or `ForwardRef` | The component is wrapped by `memo` or `forwardRef`. |
| Provider or Consumer rows | Context providers appear as their own rows, such as `ThemeContext.Provider`. |
| `Anonymous` | React could not infer a name, so you cannot tell instances apart by label. |
Keys matter more than they look. When a list re-orders, the keys tell you which row is which instance. If every row shows key="0", key="1", and so on, and the list can be re-sorted, you have probably found a bug.
An Anonymous row usually comes from an inline arrow function passed directly to memo or forwardRef. Name the inner function, as Row is named above, or set Component.displayName, and the tree becomes much easier to read.
Moving through the tree
You can navigate in four ways:
- Click a row. The right pane updates, and the matching DOM nodes are highlighted on the page while you hover.
- Use the keyboard. Up and Down move between rows. Right expands a collapsed row, and Left collapses it or moves to the parent. Click the tree once so it has focus.
- Use the element picker. Click the arrow-in-a-box icon at the top left of the tab, then click something on the page. DevTools selects the component that rendered that DOM node.
- Sync with the Elements panel. Select a DOM node in Elements, then switch to Components. The component that rendered that node is selected.
Selecting from the page is the fastest route in a large app. You usually know what is wrong on screen, such as a wrong label or a missing button, long before you know which component is responsible.
A collapsed row hides all its descendants. Holding Alt (Option on macOS) while clicking the expand arrow expands or collapses the whole subtree. This works in recent versions, so check your own build if it does nothing.
Parent versus owner
The tree shows the parent-child structure. The details pane also lists who rendered each component, the owner. These two relationships differ, and mixing them up causes real debugging mistakes.
- The parent is the component above this one in the tree.
- The owner is the component whose render function created the element with
<Component />.
In the sample app, Page sits under Layout in the tree. But Layout did not create <Page />. App did, and passed it to Layout as children. So Page's parent is Layout, and its owner is App.
This distinction matters when you ask "where does this prop come from?" A prop on Page is set in App, not in Layout. Layout cannot change it, so reading Layout will not help. Look at the "rendered by" section in the right pane. It lists the chain of owners, and you can click an entry to jump to that component.
Useful icons in the right pane
When a component is selected, the top of the right pane shows a row of icons. Their exact placement varies between versions.
- The eye icon selects the component's DOM node in the Elements panel.
- The bug icon logs the component's details to the Console.
- The source icon (angle brackets) jumps to the component's definition in the Sources panel. A later chapter covers it in detail.
Use the eye icon when you need to see the CSS applied to the output of a component. Use the Elements panel to go the other way.
Common problems
The tree is empty or the tab is missing. DevTools only detects pages where React is running. If the page uses a production build, the extension icon turns blue or red depending on the mode, and the tabs may behave differently. The build-modes chapter explains this. Also check that you did not open DevTools before the app loaded. Reload with DevTools open.
A component you expect is not listed. Possible causes:
- A component filter is hiding it. Filters can hide host nodes, or components by name or location.
- It is not rendered right now. Conditional rendering such as
{open && <Menu />}removes it from the tree completely. - It is inside a different React root, such as a portal or a separate embedded app. Multiple roots appear as separate top-level trees, so scroll the left pane.
The names are minified or meaningless. A minified production build replaces function names with short names. Debug against a development build where possible.
The selection disappears after an update. If a component unmounts, for example because its key changed, its instance is gone and DevTools cannot keep it selected. A new instance with the same name is a different instance.
Practice
Exercise
In the sample app, which component is the parent of Page in the tree, and which component is its owner? Why do they differ, and where in DevTools would you check the owner?
Show answer
Layout is the parent of Page, because Page is rendered inside Layout's output through the children prop. App is the owner, because App's render function contains the <Page /> element. You can check the owner in the "rendered by" section of the right pane when Page is selected.
Exercise
You see the text "Grace" rendered in the wrong colour on the page. List the steps to find the component responsible, using only navigation features from this chapter.
Show answer
Open the Components tab and click the element picker icon. Click "Grace" on the page, which selects the Row with key="Grace". Check its name, key and badges in the tree. Then use the rendered-by list to move up to its owners, or use the eye icon to inspect the DOM node's styles in the Elements panel.