You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package provides low-level APIs to support renderers like [React Native](https://github.com/facebook/react-native). If you're looking for the standalone React DevTools UI, **we suggest using [`react-devtools`](https://github.com/facebook/react/tree/main/packages/react-devtools) instead of using this package directly**.
4
4
5
-
This is a low-level package. If you're looking for the Electron app you can run, **use `react-devtools` package instead.**
5
+
This package provides two entrypoints: labeled "backend" and "standalone" (frontend). Both APIs are described below.
6
6
7
-
##API
7
+
#Backend API
8
8
9
-
### `react-devtools-core`
9
+
Backend APIs are embedded in _development_ builds of renderers like [React Native](https://github.com/facebook/react-native) in order to connect to the React DevTools UI.
10
10
11
-
This is similar requiring the `react-devtools` package, but provides several configurable options. Unlike `react-devtools`, requiring `react-devtools-core` doesn't connect immediately but instead exports a function:
11
+
### Example
12
+
13
+
If you are building a non-browser-based React renderer, you can use the backend API like so:
// Must be called before packages like react or react-native are imported
20
+
connectToDevTools({
21
+
...config
22
+
});
23
+
}
16
24
```
17
25
18
-
Run `connectToDevTools()` in the same context as React to set up a connection to DevTools.
19
-
Be sure to run this function *before* importing e.g. `react`, `react-dom`, `react-native`.
26
+
> **NOTE** that this API (`connectToDevTools`) must be (1) run in the same context as React and (2) must be called before React packages are imported (e.g. `react`, `react-dom`, `react-native`).
27
+
28
+
### `connectToDevTools` options
29
+
| Prop | Default | Description |
30
+
|---|---|---|
31
+
|`host`|`"localhost"`| Socket connection to frontend should use this host. |
32
+
|`isAppActive`|| (Optional) function that returns true/false, telling DevTools when it's ready to connect to React. |
33
+
|`port`|`8097`| Socket connection to frontend should use this port. |
34
+
|`resolveRNStyle`|| (Optional) function that accepts a key (number) and returns a style (object); used by React Native. |
35
+
|`retryConnectionDelay`|`200`| Delay (ms) to wait between retrying a failed Websocket connection |
36
+
|`useHttps`|`false`| Socket connection to frontend should use secure protocol (wss). |
37
+
|`websocket`|| Custom `WebSocket` connection to frontend; overrides `host` and `port` settings. |
38
+
39
+
# Frontend API
40
+
41
+
Frontend APIs can be used to render the DevTools UI into a DOM node. One example of this is [`react-devtools`](https://github.com/facebook/react/tree/main/packages/react-devtools) which wraps DevTools in an Electron app.
Renders DevTools interface into a DOM node over SSL using a custom host name (Default is localhost).
78
+
#### `connectToSocket(socket: WebSocket)`
79
+
> This is an advanced config function that is typically not used.
80
+
81
+
Custom `WebSocket` connection to use for communication between DevTools frontend and backend. Calling this method automatically initializes the DevTools UI (similar to calling `startServer()`).
82
+
83
+
#### `openProfiler()`
84
+
Automatically select the "Profiler" tab in the DevTools UI.
85
+
86
+
#### `setContentDOMNode(element: HTMLElement)`
87
+
Set the DOM element DevTools UI should be rendered into on initialization.
Copy file name to clipboardExpand all lines: packages/react-devtools-inline/README.md
+120-21
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,16 @@
1
1
# `react-devtools-inline`
2
2
3
-
React DevTools implementation for embedding within a browser-based IDE (e.g.[CodeSandbox](https://codesandbox.io/), [StackBlitz](https://stackblitz.com/)).
3
+
This package can be used to embed React DevTools into browser-based tools like[CodeSandbox](https://codesandbox.io/), [StackBlitz](https://stackblitz.com/), and [Replay](https://replay.io).
4
4
5
-
This is a low-level package. If you're looking for the standalone DevTools app, **use the `react-devtools`package instead.**
5
+
If you're looking for the standalone React DevTools UI, **we suggest using [`react-devtools`](https://github.com/facebook/react/tree/main/packages/react-devtools) instead of using this package directly**.
6
6
7
-
## Usage
7
+
---
8
+
9
+
> **Note** that this package (and the DevTools UI) relies on several _experimental_ APIs that are **only available in the [experimental release channel](https://reactjs.org/docs/release-channels.html#experimental-channel)**. This means that you will need to install `react@experimental` and `react-dom@experimenal`.
10
+
11
+
---
12
+
13
+
# Usage
8
14
9
15
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`<sup>1</sup>).
10
16
@@ -16,15 +22,18 @@ The frontend and backend can be initialized in any order, but **the backend must
16
22
17
23
<sup>1</sup> Sandboxed iframes are supported.
18
24
19
-
## API
25
+
# Backend APIs
26
+
### `initialize(windowOrGlobal)`
27
+
28
+
Installs the global hook on the window/global object. This hook is how React and DevTools communicate.
20
29
21
-
### `react-devtools-inline/backend`
30
+
> **This method must be called before React is loaded.** (This includes `import`/`require` statements and `<script>` tags that include React.)
31
+
32
+
### `activate(windowOrGlobal)`
22
33
23
-
***`initialize(contentWindow)`** -
24
-
Installs the global hook on the window. This hook is how React and DevTools communicate. **This method must be called before React is loaded.**<sup>2</sup>
25
-
***`activate(contentWindow)`** -
26
34
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-initialization events.
<sup>2</sup> The backend must be initialized before React is loaded. (This means before any `import` or `require` statements or `<script>` tags that include React.)
53
+
# Frontend APIs
45
54
46
-
### `react-devtools-inline/frontend`
55
+
### `initialize(windowOrGlobal)`
56
+
Configures the DevTools interface to listen to the `window` (or `global` object) the backend was injected into. This method returns a React component that can be rendered directly.
47
57
48
-
***`initialize(contentWindow)`** -
49
-
Configures the DevTools interface to listen to the `window` the backend was injected into. This method returns a React component that can be rendered directly<sup>3</sup>.
58
+
> Because the DevTools interface makes use of several new React concurrent features (like Suspense) **it should be rendered using `ReactDOMClient.createRoot` instead of `ReactDOM.render`.**
<sup>3</sup> Because the DevTools interface makes use of several new React APIs (e.g. suspense, concurrent mode) it should be rendered using `ReactDOMClient.createRoot`. **It should not be rendered with `ReactDOM.render`.**
64
-
65
-
## Examples
73
+
# Advanced examples
66
74
67
75
### Supporting named hooks
68
76
@@ -169,7 +177,7 @@ iframe.onload = () => {
169
177
};
170
178
```
171
179
172
-
### Advanced integration with custom "wall"
180
+
### Advanced: Custom "wall"
173
181
174
182
Below is an example of an advanced integration with a website like [Replay.io](https://replay.io/) or Code Sandbox's Sandpack (where more than one DevTools instance may be rendered per page).
175
183
@@ -235,25 +243,116 @@ const wall = {
235
243
};
236
244
```
237
245
238
-
## Local development
246
+
### Advanced: Node + browser
247
+
248
+
Below is an example of an advanced integration that could be used to connect React running in a Node process to React DevTools running in a browser.
249
+
250
+
##### Sample Node backend
251
+
```js
252
+
const {
253
+
activate,
254
+
createBridge,
255
+
initialize,
256
+
} =require('react-devtools-inline/backend');
257
+
const { createServer } =require('http');
258
+
constSocketIO=require('socket.io');
259
+
260
+
constserver=createServer();
261
+
constsocket=SocketIO(server, {
262
+
cors: {
263
+
origin:"*",
264
+
methods: ["GET", "POST"],
265
+
allowedHeaders: [],
266
+
credentials:true
267
+
}
268
+
});
269
+
socket.on('connection', client=> {
270
+
constwall= {
271
+
listen(listener) {
272
+
client.on('message', data=> {
273
+
if (data.uid===UID) {
274
+
listener(data);
275
+
}
276
+
});
277
+
},
278
+
send(event, payload) {
279
+
constdata= {event, payload, uid:UID};
280
+
client.emit('message', data);
281
+
},
282
+
};
283
+
284
+
constbridge=createBridge(global, wall);
285
+
286
+
client.on('disconnect', () => {
287
+
bridge.shutdown();
288
+
});
289
+
290
+
activate(global, { bridge });
291
+
});
292
+
socket.listen(PORT);
293
+
```
294
+
295
+
##### Sample Web frontend
296
+
```js
297
+
import { createElement } from'react';
298
+
import { createRoot } from'react-dom/client';
299
+
import {
300
+
createBridge,
301
+
createStore,
302
+
initializeascreateDevTools,
303
+
} from'react-devtools-inline/frontend';
304
+
import { io } from"socket.io-client";
305
+
306
+
let root =null;
307
+
308
+
constsocket=io(`http://${HOST}:${PORT}`);
309
+
socket.on("connect", () => {
310
+
constwall= {
311
+
listen(listener) {
312
+
socket.on("message", (data) => {
313
+
if (data.uid===UID) {
314
+
listener(data);
315
+
}
316
+
});
317
+
},
318
+
send(event, payload) {
319
+
constdata= { event, payload, uid:UID };
320
+
socket.emit('message', data);
321
+
},
322
+
};
323
+
324
+
constbridge=createBridge(window, wall);
325
+
conststore=createStore(bridge);
326
+
constDevTools=createDevTools(window, { bridge, store });
You can also build and test this package from source.
240
339
241
-
###Prerequisite steps
340
+
## Prerequisite steps
242
341
DevTools depends on local versions of several NPM packages<sup>1</sup> also in this workspace. You'll need to either build or download those packages first.
243
342
244
343
<sup>1</sup> Note that at this time, an _experimental_ build is required because DevTools depends on the `createRoot` API.
245
344
246
-
####Build from source
345
+
### Build from source
247
346
To build dependencies from source, run the following command from the root of the repository:
248
347
```sh
249
348
yarn build-for-devtools
250
349
```
251
-
####Download from CI
350
+
### Download from CI
252
351
To use the latest build from CI, run the following command from the root of the repository:
253
352
```sh
254
353
./scripts/release/download-experimental-build.js
255
354
```
256
-
###Build steps
355
+
## Build steps
257
356
Once the above packages have been built or downloaded, you can watch for changes made to the source code and automatically rebuild by running:
0 commit comments