Sub topic 3
Navigating the component tree
This chapter assumes you can write React function components, use useState and props, and open the browser's Elements and Console panels. It also assumes you have React Developer Tools installed and a React app running in development mode.
What the Components tab shows
The Elements panel shows the DOM. The Components tab shows the React tree: the components your code defined, nested the way they render each other. A single <List> component can produce dozens of DOM nodes, but it appears as one entry in this tree.
Open the tab and you will see the root at the top. Each row is one component. Indentation shows parent and child relationships. Children appear below their parent and are indented one level deeper.
Reading a row
Each row has a name and sometimes small badges next to it.
- Name. This is the function name or the
displayNameof the component. Anonymous components can show up asAnonymous, so give your components names. - Key. If a component has a
key, it appears askey="..."next to the name. In a list, this helps you tell sibling items apart. - Badges. Wrappers such as
MemoandForwardRefappear as labels. A memoized component shows aMemobadge, which matters later when you look at re-renders.
Host elements such as div and li are hidden by default. The tree focuses on components, not on DOM tags.
Moving through the tree
You can navigate with the mouse or the keyboard.
- Click a row to select it. The right pane then shows its props, state, and hooks. The next chapter covers that pane in detail.
- Press the up and down arrow keys to move between visible rows.
- Press the right arrow to expand a collapsed component and the left arrow to collapse it.
- Hold Alt (Option on macOS) while clicking the expand arrow to expand every descendant at once.
Collapsing branches you do not care about keeps a large tree readable.
Selecting from the page
You do not have to hunt for a component by name. Use the select element icon, the small pointer in the top-left of the DevTools panel. Click it, then click any element on the page. The Components tab jumps to the nearest component that rendered that element.
This is the fastest route when you can see the problem on screen but do not know which component draws it.
You can also go the other direction. Selecting a component and clicking the eye icon in the toolbar selects its DOM node in the Elements panel. The bug icon logs the component's data to the Console.
Following the path to the root
Selecting a component also shows its ancestors. Look at the path of parents above it to see which components pass data down. This helps you answer questions such as "Which parent supplies this prop?" and "Is this component rendered inside a context provider?"
Context providers and consumers appear as their own rows. Seeing a Context.Provider above your component confirms that it can read that context.
Common mistakes
- Looking for DOM tags. If you search for a
div, you will not find it. Host elements are hidden by default. - Expecting source order. The tree follows render output. Conditional rendering changes which rows exist from moment to moment.
- Missing the tab. If the Components tab is empty or missing, the page may not be running React, or the build may block DevTools. Check that you are on a development build.
Exercise
You can see a broken button on the page but do not know which component renders it. Which two steps in the Components tab get you to that component fastest?
Show answer
Click the select element icon in the DevTools toolbar, then click the button on the page. The Components tab jumps to the nearest component that rendered it. Check the ancestor path above it to see its parents.