Sub topic 6
Finding components with search and filters
Real apps have hundreds or thousands of components in the tree. Scrolling and expanding nodes by hand does not work at that size. This chapter covers the two tools that shrink the Components tab to what you need: the search box and component filters.
This chapter assumes you can write function components, use useState and props, and have React Developer Tools installed and open on a development build. It also assumes you know how to select a component in the Components tab.
Search and filters do different jobs
Search and filters can look alike, but they work differently.
- Search leaves the tree as it is. It highlights matches, expands their ancestors, and lets you jump from match to match.
- Filters change what the tree contains. Components that match an enabled filter are removed from the view.
Use search when you know what you are looking for. Use filters when the tree is full of components you never want to see, such as library internals or DOM elements.
Using the search box
The search box sits at the top of the Components tab. Click it, or focus it with the keyboard, and type part of a component name. The tree responds right away:
- Components whose names contain your text are highlighted.
- Ancestors of a match expand so the match is visible.
- A counter shows your position among the matches, for example
2 | 7. - The up and down arrow buttons move to the previous or next match. Enter and Shift+Enter do the same from the keyboard.
Each match is also selected, so the right-hand panel shows its props, state, and hooks. You can type Item, press Enter until you reach the instance you care about, and read its values without clicking in the tree.
Case and regular expressions
Two toggle buttons sit inside the search box:
| Toggle | Effect | Example | Column |
|---|---|---|---|
| Match case | Makes the search case-sensitive. It is off by default. | `Item` no longer matches `ListItem`'s lowercase uses such as `item` | |
| Use regular expression | Treats the text as a regular expression. | `^(Header\ | Footer)$` matches only those two names |
Regular expressions help when a naming convention groups related components. For example, Provider$ finds every context provider, and ^Settings finds every component in a settings feature. If a regular expression is invalid, the search finds nothing, so check for an unclosed group or a stray escape.
What search matches
Search matches the display name that DevTools shows in the tree. It does not search your source code, and it does not search prop values. This leads to common problems:
- A component shows as
Anonymous, so searching for the name you wrote in the file finds nothing. - A name was minified in a production build, so you see short names like
tore. - A wrapper such as
memoorforwardRefadds a badge to the node. You search the underlying component's name, not the badge.
React infers a display name from the function name. An anonymous default export has no name to infer, so it shows up as Anonymous.
import { memo } from 'react';
// Shows as "Anonymous" in the tree.
export default memo(function ({ label }) {
return <li>{label}</li>;
});
// Shows as "Row" with a Memo badge. Searching for "Row" finds it.
export const Row = memo(function Row({ label }) {
return <li>{label}</li>;
});
// A component created by a factory can be named explicitly.
const Generated = () => <li>generated</li>;
Generated.displayName = 'GeneratedRow';
export { Generated };Naming your components is the cheapest way to improve searching. Prefer named functions over anonymous ones, and set displayName on components created inside factories or higher-order components.
Finding a component from the page
You do not have to start from a name. If you can see the UI element that is wrong, start there:
- Click the select-element-on-page button (the target icon) at the top left of the Components tab.
- Hover over the page. DevTools outlines the element under the cursor.
- Click the element. The Components tab selects the component that rendered it and expands the tree to show it.
You can also select a DOM node in the browser's Elements panel and then switch to the Components tab. DevTools selects the nearest component that rendered that node.
Both methods are fast when you do not know the component's name, such as in code you did not write.
Filtering the tree
Open the settings (the gear icon at the top right of the DevTools panel) and go to the Components section. There you can define filters that hide components from the tree. Each filter can be turned on and off with a checkbox, so you can keep a filter defined without applying it.
Filters come in a few kinds. The exact labels differ a little between versions, but these are the common ones:
| Filter kind | What it hides | Typical use |
|---|---|---|
| Type | Whole categories, such as host components (DOM elements like `div` and `li`), context providers and consumers, or `memo` and `forwardRef` wrappers | Show only your own function and class components |
| Location | Components whose source file is in a given place, such as `node_modules` | Hide third-party library internals |
| Name | Components whose name matches a regular expression | Hide a noisy wrapper that appears on every screen |
By default, DevTools hides host components. This is why you usually see List and Item in the tree and not ul and li.
A practical setup
For debugging an application you wrote, this combination works well:
- Keep the host component filter on, so only React components appear.
- Add a location filter for
node_modules, so routing, styling, and state libraries stop filling the tree with wrappers. - Add a name filter for any wrapper you wrote that adds no information, such as a layout shell that appears on every screen.
Filters apply when DevTools builds the tree. A hidden component has no node, so you cannot select it, search for it, or read its props. If a component seems to be missing, check your filters first.
Filters and library code
A location filter on node_modules removes components from libraries, but it does not remove their effect on your code. A hidden context provider still passes its value down, and a hidden wrapper still re-renders its children. When a bug looks like it comes from "nowhere", turn the library filter off and look at the components between your own.
Combining search and filters
The two tools work together. Filters reduce the tree, and search finds one component in what remains. Combining them also reduces false matches. Searching for Button in an unfiltered tree can match a dozen library components, but with the node_modules filter on, only your buttons are left.
A good routine for an unfamiliar screen:
- Click the select-element-on-page button and pick the broken UI. This gives you one component and its position in the tree.
- Read the names of its ancestors. They show you how the screen is built.
- Search for a parent's name to find other instances of the same component and compare their props.
- Add a filter if the same noise keeps getting in the way.
Common problems
- The search finds nothing. Check that "Match case" is off, the regular expression toggle matches your input, and the component is not anonymous or hidden by a filter.
- The count is larger than expected. Search matches partial names, so
Itemalso matchesMenuItemandItemList. Use a regular expression such as^Item$for an exact match. - The tree shows minified names. You are probably looking at a production build. Use a development build or a profiling build, which the build-modes chapter covers.
- A component appears in the app but not in the tree. A filter is hiding it, or it is a host element, which is hidden by default.
- You changed filters and the selection disappeared. If the selected component is now hidden, DevTools clears the selection. Turn the filter back off to select it again.
Exercise
A memo-wrapped list row is exported as export default memo(({ label }) => <li>{label}</li>). You search for the file name Row in the Components tab and get no results. Give two reasons this can happen and one change that fixes both.
Show answer
The arrow function has no name, so the node shows as Anonymous and there is no Row text to match. A filter for the file's location or a name filter could also be hiding it. Giving the function a name (memo(function Row({ label }) { ... }), or setting displayName = 'Row') fixes the naming problem, and you should also check that no enabled filter hides it.
Exercise
You want a search that finds only Header and Footer and ignores components like HeaderNav. What do you turn on, and what do you type?
Show answer
Turn on the "Use regular expression" toggle and type ^(Header|Footer)$. The anchors ^ and $ make the pattern match whole names only.