A standalone bundle that exposes React DevTools utilities for querying the React component tree. Designed for use with Playwright, Puppeteer, or any tool that can inject JavaScript into a page.
Installation
yarn workspace react-devtools-api build
Usage
Injecting via Playwright
import { chromium } from 'playwright';import fs from 'fs';const browser = await chromium.launch();const page = await browser.newPage();// Load the bundleconst devtoolsApi = fs.readFileSync( 'path/to/react-devtools-api/dist/react-devtools-api.js', 'utf-8');// Navigate to your React appawait page.goto('http://localhost:3000');// Inject the DevTools APIawait page.evaluate(devtoolsApi);// Wait for React to be readyawait
API Reference
Finding Components
const api = window.__REACT_DEVTOOLS_API__;// Find component at a DOM elementapi.getComponentForElement(document.querySelector('.my-button'));// { id: 5, rendererID: 1, name: 'Button', path: [...] }// Find component by CSS selectorapi.getComponentForSelector('.my-button');// { id: 5, rendererID: 1, name: 'Button', path: [...] }// Find all components by name (partial match)api.findComponentsByName('Button');// [{ id: 5, ... }, { id: 12, ... }]// Find all components by name (exact match)api.findComponentsByName('Button', true);// Find components by regexapi.findComponentsByPattern(/^User/);// [{ id: 3, name: 'UserList' }, { id: 7, name: 'UserProfile' }]
Inspecting Components
// Get detailed component info (props, state, hooks, context)api.inspectComponent(5);// {// id: 5,// name: 'Button',// type: 'function',// props: { onClick: ƒ, children: 'Click me' },// state: null,// hooks: [{ name: 'State', value: false }],// context: null,// key: null,// owners: [{ id: 3, displayName: 'App' }]// }// Get DOM elements for a componentapi.getDOMForComponent(5);// [<button class="my-button">Click me</button>]// Get owner chain (parent components)api.getOwners(5);// [{ id: 3, displayName: 'App' }, { id: 1, displayName: 'Root' }]// Get full component tree as stringapi.getComponentTree();// └── App (id: 1)
Debugging Utilities
// Log component to browser consoleapi.logComponent(5);// Highlight a component in the page (2 second overlay)api.highlightComponent(5);// Clear all highlightsapi.clearHighlights();
Status Checks
// Check if DevTools hook is available and React is mountedapi.isAvailable();// true// Wait for React to be ready (with timeout)await api.waitForReact(10000);
Advanced
// Get the raw DevTools hookapi.getHook();// window.__REACT_DEVTOOLS_GLOBAL_HOOK__// Get the primary renderer interfaceapi.getRenderer();// { rendererID: 1, renderer: RendererInterface }// Get all renderer interfaces (for apps with multiple React roots)api.getAllRenderers();// [{ rendererID: 1, renderer: ... }, { rendererID: 2, renderer: ... }]