@@ -74,6 +74,8 @@ const { contentWindow } = iframe;
74
74
initializeBackend (contentWindow);
75
75
76
76
// React application can be injected into <iframe> at any time now...
77
+ // Note that this would need to be done via <script> tag injection,
78
+ // as setting the src of the <iframe> would load a new page (withou the injected backend).
77
79
78
80
// Initialize DevTools UI to listen to the hook we just installed.
79
81
// This returns a React component we can render anywhere in the parent window.
@@ -91,29 +93,33 @@ Sandboxed `iframe`s are also supported but require more complex initialization.
91
93
92
94
** ` iframe.html ` **
93
95
``` js
94
- import { activate , initialize } from ' react-devtools-inline/backend' ;
96
+ import { activate , initialize } from " react-devtools-inline/backend" ;
95
97
96
98
// The DevTooks hook needs to be installed before React is even required!
97
99
// The safest way to do this is probably to install it in a separate script tag.
98
100
initialize (window );
99
101
100
102
// Wait for the frontend to let us know that it's ready.
101
- window . addEventListener ( ' message ' , ({ data }) => {
103
+ function onMessage ({ data }) {
102
104
switch (data .type ) {
103
- case ' activate' :
105
+ case " activate-backend" :
106
+ window .removeEventListener (" message" , onMessage);
107
+
104
108
activate (window );
105
109
break ;
106
110
default :
107
111
break ;
108
112
}
109
- });
113
+ }
114
+
115
+ window .addEventListener (" message" , onMessage);
110
116
```
111
117
112
118
** ` main-window.html ` **
113
119
``` js
114
- import { initialize } from ' react-devtools-inline/frontend' ;
120
+ import { initialize } from " react-devtools-inline/frontend" ;
115
121
116
- const iframe = document .getElementById (' target' );
122
+ const iframe = document .getElementById (" target" );
117
123
const { contentWindow } = iframe;
118
124
119
125
// Initialize DevTools UI to listen to the iframe.
@@ -126,9 +132,9 @@ const DevTools = initialize(contentWindow);
126
132
iframe .onload = () => {
127
133
contentWindow .postMessage (
128
134
{
129
- type: ' activate' ,
135
+ type: " activate-backend "
130
136
},
131
- ' * '
137
+ " * "
132
138
);
133
139
};
134
140
```
0 commit comments