feat(react-devtools): inspect React components and profile renders through the Metro inspector

Merged
TH
1mo ago
+6223-1257

Summary

agent-device react-devtools is now built-in React Native component inspection and render profiling. Every command attaches to the app's JavaScript runtime through its Metro dev server (CDP Runtime.evaluate) and drives the React DevTools backend that development builds already ship. The app, Metro config and Babel config need no changes, and there is no helper to start or wait for.

Components

$ agent-device react-devtools find ContactRow --exact --limit 1
60 mounted components match "ContactRow":
  @c1213 ContactRow [memo] key=.$0 in ContactList < MemoDefeatedScenario
  +59 more (--limit 60)
$ agent-device react-devtools inspect PerfLabScreen
@c1169 PerfLabScreen key=memo-defeated
rendered by: PerfLabRoute(./perf-lab.tsx) @c1168 < WrappedScreenComponent @c1167 < Route(perf-lab) @c1158 < SceneView @c1148 < …
props:
  initialScenario: "memo-defeated"
hooks:
  State: "memo-defeated"
  State: 0
  State: "idle"
$ agent-device react-devtools errors
1 mounted component logged errors or warnings while rendering:
  @c14 ContextNavigator: 2 warnings
    warning: `expo-router/unstable-native-tabs` is deprecated. Use `expo-router/native-tabs` instead.
  • tree [@ref|Name] [--depth] [--limit] prints the mounted tree with @c refs. Wrapper chains such as providers collapse onto one line, and runs of three or more same-shape siblings (list rows) into one counted line, so the 1,510-component Expo Router test app fits in about 25 lines.
  • find <Name> [--exact] lists matches with the components that rendered them.
  • inspect <@ref|Name> prints owners, props, state, hooks and logged problems. A name that several components share fails closed and lists their refs.
  • errors lists components that logged console errors or warnings while rendering, with the messages.

The tree comes from the backend's operations stream (names, keys, child order, error and warning counts). Props, state and hooks come from its inspectElement. When another DevTools frontend owns that stream, the tree commands fail with tree-unavailable instead of returning a partial tree.

Render profiling

$ agent-device react-devtools profile start
$ agent-device react-devtools profile stop
React profile 4.7s: 5 commits, 2091ms rendering, 5 over 16ms (worst #1 438ms)
1. memo-defeated ContactRow: memo bypassed on 300/300 updates; new highlight (object) 300x, onSelect (function) 300x from ContactList -> wrap those props in useCallback/useMemo in ContactList
More: profile report [Component] | profile commit [N]
  • stop prints ranked findings and saves the raw recording.
  • report and commit re-analyze the saved recording, so no view loses detail another trimmed.
  • report with no name lists components by self time with why they rendered; report <Component> shows each variant (Row and memo Row stay separate) with its findings.
  • commit with no index lists commits in order (the slowest ones when capped); commit <N> lists every component that rendered in it.

Findings carry the component kind, so a finding about memo Row never explains a slow commit spent in plain Row. A renderer whose profiling data cannot be read fails stop with collect-failed, or adds a note when other renderers were read.

Each subcommand rejects options it does not read. If one app is connected from several devices, every command fails closed until --metro-device names one.

Removed: the npm exec agent-react-devtools passthrough, its 8097 remote tunnel and port reverse, and the injected-dispatch seam only that port reverse used.

Validation

At 4178ccd7b1, check:affected --run (4,589 tests), check:production-exports and check:di-seams pass.

SetupWhat ranResult
Android test app, RN 0.88-rc (Expo Router)tree, find, inspect by ref and name, errors, ambiguous names, inspect during a profile, all profile viewspass
iOS simulator, bare RN 0.83.1every inspection command, plus all 7 Perf lab scenarios through profile start/stop/report/commitpass, each scenario's expected finding
iOS simulator, bare RN 0.83.1 with react-devtools-core 8.0.0 swapped in (no RN release ships DevTools 7+ yet; 0.88 and the 0.89 nightly pin ^6.1.5)tree, find, inspect, errors, profile with mounts while recordingpass, with operations naming. The previous build failed tree here; 6.1.5 rechecked after the change
Android, bare RN 0.76.9tree, inspect (hooks), errors, profilepass; errors notes that the list can be incomplete
Android, bare RN 0.74.7tree, find, inspect, errors, profile (memo-defeated)pass; same errors note
agent-device proxy, client with its own state dirprofile start/stop, drill-downspass; the saved profile stays on the client
Remote bridge: agent and Metro in a Linux container (colima), emulator and daemon on the macOS host, app traffic through a local companion bridgeconnect --remote-config, open, inspection and profiling from the containerpass, through the session's Metro binding

Found and fixed while verifying:

  • Debugger URL behind a tunnel. A tunnel shows Metro its own loopback address, which Metro then puts in webSocketDebuggerUrl. The CDP socket now opens on the server that listed the target.
  • Metro choice on a bridge. Inspection and profiling use explicit flags, then the session's Metro binding, then localhost:8081. They never use a device-facing URL (metroPublicBaseUrl or the bridge).
  • errors on older backends. React DevTools 5 (RN 0.74 and 0.76) does not attribute every console call to a component, so errors now says its list can be incomplete instead of claiming nothing was logged.

By design, not changed: Metro accepts the debugger socket only from its own machine (local socket and loopback Origin). The companion bridge opens upstream WebSockets without an Origin, so Metro refuses debugger sockets sent through the bridge. That keeps the bridge from exposing JavaScript execution in the app, so the companion stays as it is. The removed 8097 tunnel did something else: it carried the app's DevTools backend to a frontend on the host.