Sub topic 14
Standalone DevTools for React Native and embedded views
This chapter assumes you have used the browser extension's Components and Profiler tabs, as covered in the earlier chapters. You should also be comfortable running a local dev server and using a terminal.
The browser extension only works inside a browser that supports extensions. Many React apps run elsewhere: a React Native app on a phone, a page inside a native WebView, an Electron window, or Safari. For these cases React ships a standalone DevTools app. It has the same Components and Profiler tabs, but it runs as its own window and your app connects to it over a WebSocket.
How the standalone app works
The browser extension injects a hook into the page. React reports its component tree to that hook, and the extension reads it. The standalone app uses the same hook and the same frontend. The difference is the transport: the app opens a WebSocket connection to a server inside the standalone window instead of talking to a browser extension.
The direction matters. The DevTools window listens, and your app connects to it. If the app starts first and DevTools is not listening yet, the app keeps retrying. The connection usually succeeds once the window opens.
Installing and starting the standalone app
You can install the package globally or run it once with npx.
# Option 1: install globally
npm install -g react-devtools
react-devtools
# Option 2: run without installing
npx react-devtoolsA window opens with the message "Waiting for React to connect". It listens on port 8097 by default. When an app connects, the component tree appears.
Debugging React Native
React Native is the most common use. The standalone app shows your component tree, props, state, and hooks, just as in the browser.
Start the DevTools window first, then start your app in development mode:
npx react-devtoolsIn a second terminal, run your usual dev command, such as npx react-native start or npx expo start, and open the app on a simulator or device.
How the connection happens depends on where the app runs.
| Where the app runs | What to do |
|---|---|
| iOS simulator | Usually nothing. The simulator shares your machine's `localhost`. |
| Android emulator | Usually nothing, but run the port forward below if the connection fails. |
| Physical Android device over USB | Forward the port with `adb reverse tcp:8097 tcp:8097`. |
| Physical iOS device or device over Wi-Fi | Make sure the device can reach your computer. Check firewall rules and that both are on the same network. |
For a USB-connected Android device, the port forward looks like this:
adb reverse tcp:8097 tcp:8097Without it, the phone's localhost points at the phone itself, so the app never reaches your computer.
What differs from the browser
- There is no Elements tab. Native views are not DOM nodes. The Components tab lists React Native components such as
View,Text, and your own components. - Selecting a component can highlight it on the device. The standalone app has an inspect control in the toolbar. When you use it, you tap a view on the device and DevTools selects the matching component.
- Style props show resolved values. React Native styles often come from
StyleSheet, so the props panel may show the style as an object or a list of objects. Read the final merged values carefully before concluding a style is missing. - The Profiler works the same way. Record, interact with the app, stop, and read the flamegraph and ranked charts. Remember that timings on an emulator or a dev-mode build are slower than a release build on real hardware. Use the numbers to compare commits and find extra renders, not as absolute performance figures.
Debugging embedded views with a script tag
A WebView or other embedded browser often cannot run extensions. For a web page loaded in such a view, you can load the DevTools backend from the standalone app itself. The standalone window serves a script at http://localhost:8097.
Add this tag to your HTML before any script that loads React:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Embedded app</title>
<!-- Development only. Load this before React so the hook exists first. -->
<script src="http://localhost:8097"></script>
</head>
<body>
<div id="root"></div>
<script src="/bundle.js"></script>
</body>
</html>React looks for the DevTools hook when it loads. If React loads before the hook exists, DevTools never sees it. That ordering problem is the most common reason a page shows "Waiting for React to connect" forever.
If the page is served from a different machine than the one running DevTools, localhost points at the wrong place. Use the IP address or hostname of the machine running the DevTools window, and make sure port 8097 is reachable from there.
Connecting from JavaScript with react-devtools-core
If you control the app's entry file, you can connect from code instead of a script tag. The react-devtools-core package provides a connectToDevTools function. It suits Electron apps, custom shells, and embedded runtimes.
npm install --save-dev react-devtools-core// index.js — the first import in your entry file.
// The connection must be set up before React loads.
import { connectToDevTools } from 'react-devtools-core';
if (process.env.NODE_ENV !== 'production') {
connectToDevTools({
host: 'localhost',
port: 8097,
});
}
import('./app');Two points about this example. First, ES module imports run in order, but a static import './app' would still be hoisted above the if block. The example therefore loads the app with a dynamic import() after the connection call. Second, the NODE_ENV check keeps the connection out of production bundles when your bundler replaces that variable at build time.
Build modes still apply
The standalone app does not change the rules from the build modes chapter. Production builds of React strip most of the debugging support. If you connect a production build, you will see little or nothing. For profiling, use a profiling build or a development build, depending on your platform. In React Native, profile in a development build for correctness checks and use a release build to judge real speed.
Common problems
- The window stays on "Waiting for React to connect". The app is not reaching port 8097. Check that the DevTools window is open, that the app is a development build, and that the host and port are right. For Android over USB, run
adb reverse. - The port is already in use. Another copy of DevTools may be running. Close it, then start the app again.
- The tree appears but updates stop. The app may have reloaded and lost the connection. Reload the app again, or restart the DevTools window if it does not reconnect.
- Components show as
Anonymous. The bundler or minifier removed function names. Use a development build so display names survive. - Several apps connect. The standalone app shows one connected app at a time. Debug one app per DevTools window.
When to use which tool
| Situation | Tool |
|---|---|
| Web app in Chrome, Edge, or Firefox | Browser extension |
| React Native app | Standalone app or the built-in debugger |
| Page in a WebView or Safari | Standalone app with the script tag |
| Electron or a custom shell | Standalone app with `connectToDevTools` |
Exercise
Your React Native app runs on a physical Android phone connected by USB. The standalone DevTools window shows "Waiting for React to connect" even though the app is running in development mode. What is the likely cause, and what command fixes it?
Show answer
The phone's localhost refers to the phone, not your computer, so the app cannot reach the DevTools server on port 8097. Run adb reverse tcp:8097 tcp:8097 to forward the port from the phone to your computer, then reload the app.
Exercise
You add <script src="http://localhost:8097"></script> at the end of the body, after your React bundle. The page loads, but DevTools never connects. Why, and how do you fix it?
Show answer
React checks for the DevTools hook when it loads. The script placed after the bundle runs too late, so React has already started without the hook. Move the tag into the head, before the script that loads React.