What is react-devtools-inline?
The react-devtools-inline package provides a way to embed React DevTools into your own applications. It allows developers to inspect and debug React component trees directly within their applications, without needing to rely on external browser extensions.
What are react-devtools-inline's main functionalities?
Embedding React DevTools
This feature allows you to embed the React DevTools directly into your application. By using the `createDevTools` function, you can create a DevTools component that can be rendered within your app, providing the same inspection and debugging capabilities as the standalone React DevTools.
import { createDevTools } from 'react-devtools-inline';
const DevTools = createDevTools();
function App() {
return (
<div>
<h1>My React App</h1>
<DevTools />
</div>
);
}
Other packages similar to react-devtools-inline
react-devtools
The react-devtools package is the standalone version of React DevTools that can be used as a browser extension or a standalone app. It provides similar functionality to react-devtools-inline but is not designed to be embedded within applications. Instead, it is used externally to inspect and debug React applications running in the browser.
redux-devtools-extension
Redux DevTools Extension is a tool for debugging application state changes in Redux applications. While it is not specifically for React component trees, it provides similar debugging capabilities for Redux state management, allowing developers to track and inspect state changes over time. It is often used alongside React DevTools for a comprehensive debugging experience.
react-devtools-inline
React DevTools implementation for embedding within a browser-based IDE (e.g. CodeSandbox, StackBlitz).
This is a low-level package. If you're looking for the standalone DevTools app, use the react-devtools
package instead.
Usage
This package exports two entry points: a frontend (to be run in the main window
) and a backend (to be installed and run within an iframe
1).
The frontend and backend can be initialized in any order, but the backend must not be activated until after the frontend has been initialized. Because of this, the simplest sequence is:
- Frontend (DevTools interface) initialized in the main
window
. - Backend initialized in an
iframe
. - Backend activated.
1 Sandboxed iframes are supported.
API
react-devtools-inline/backend
initialize(contentWindow)
-
Installs the global hook on the window. This hook is how React and DevTools communicate. This method must be called before React is loaded. (This means before any import
or require
statements!)activate(contentWindow)
-
Lets the backend know when the frontend is ready. It should not be called until after the frontend has been initialized, else the frontend might miss important tree-initialiation events.
import { activate, initialize } from 'react-devtools-inline/backend';
initialize();
activate();
react-devtools-inline/frontend
initialize(contentWindow)
-
Configures the DevTools interface to listen to the window
the backend was injected into. This method returns a React element that can be rendered directly.
import { initialize } from 'react-devtools-inline/frontend';
const iframe = document.getElementById(frameID);
const contentWindow = iframe.contentWindow;
const DevTools = initialize(contentWindow);
Examples
Configuring a same-origin iframe
The simplest way to use this package is to install the hook from the parent window
. This is possible if the iframe
is not sandboxed and there are no cross-origin restrictions.
import {
activate as activateBackend,
initialize as initializeBackend
} from 'react-devtools-inline/backend';
import { initialize as initializeFrontend } from 'react-devtools-inline/frontend';
const iframe = document.getElementById('target');
const { contentWindow } = iframe;
initializeBackend(contentWindow);
const DevTools = initializeFrontend(contentWindow);
activateBackend(contentWindow);
Configuring a sandboxed iframe
Sandboxed iframe
s are also supported but require more complex initialization.
iframe.html
import { activate, initialize } from 'react-devtools-inline/backend';
initialize(window);
window.addEventListener('message', ({ data }) => {
switch (data.type) {
case 'activate':
activate(window);
break;
default:
break;
}
});
main-window.html
import { initialize } from 'react-devtools-inline/frontend';
const iframe = document.getElementById('target');
const { contentWindow } = iframe;
const DevTools = initialize(contentWindow);
iframe.onload = () => {
contentWindow.postMessage(
{
type: 'activate',
},
'*'
);
};