Skip to content

Commit 1340a97

Browse files
Kent WaltersKent Walters
authored andcommitted
Fix invalid hook error.
1 parent aec3a32 commit 1340a97

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export default App;
3636

3737
`<Retool>` will accept an optional `data` object, which is made available to the embedded application. When an embedded Retool application runs a Parent Window Query, `<Retool>` will check if `data` contains a key matching the Parent Window Query's selector, and if so, return that value to the query.
3838

39+
`<Retool>` will accept optional `height` and `width` props which will be used for the dimensions of the embedded window.
40+
41+
`<Retool>` will accept an optional `onData` callback that will be called with the data of an event that is sent from the embedded Retool app. These events can be sent from a JavaScript query inside of Retool by using the `parent.postMessage()` syntax.
42+
3943
### Example
4044

4145
Running `yarn start` will start an application with a basic Retool app embeded.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
]
2525
]
2626
},
27-
"dependencies": {
27+
"peerDependencies": {
2828
"react": "^17.0.2",
2929
"react-dom": "^17.0.2"
3030
},

src/App.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Retool from "./components/Retool";
2-
import { useState, useEffect } from "react";
2+
import { useState } from "react";
33

44
const App = () => {
55
const sample = {
@@ -8,21 +8,6 @@ const App = () => {
88
input: "",
99
};
1010

11-
useEffect(() => {
12-
const handler = (event) => {
13-
if (
14-
event.origin === "https://support.retool.com" &&
15-
event.data?.type !== "PARENT_WINDOW_QUERY"
16-
) {
17-
setRetoolData(event.data);
18-
}
19-
};
20-
21-
window.addEventListener("message", handler);
22-
23-
return () => window.removeEventListener("message", handler);
24-
}, []);
25-
2611
const [retoolData, setRetoolData] = useState("");
2712
const [data, setData] = useState(sample);
2813
return (
@@ -50,8 +35,9 @@ const App = () => {
5035
data={data}
5136
height="700px"
5237
width="1000px"
38+
onData={setRetoolData}
5339
></Retool>
54-
<h1> {retoolData} </h1>
40+
<h1> {JSON.stringify(retoolData)} </h1>
5541
</div>
5642
);
5743
};

src/components/Retool.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useRef, useState } from "react";
22

3-
const Retool = ({ data, url, height, width }) => {
3+
const Retool = ({ data, url, height, width, onData }) => {
44
const embeddedIframe = useRef(null);
55
const [elementWatchers, setElementWatchers] = useState({});
66

@@ -25,6 +25,15 @@ const Retool = ({ data, url, height, width }) => {
2525
/* Handle events - if PWQ then create/replace watchers -> return result */
2626
const handler = (event) => {
2727
if (!embeddedIframe?.current?.contentWindow) return;
28+
29+
if (
30+
event.origin === new URL(url).origin &&
31+
event.data?.type !== "PARENT_WINDOW_QUERY" &&
32+
event.data?.type !== 'intercom-snippet__ready'
33+
) {
34+
onData(event.data);
35+
}
36+
2837
if (event.data.type === "PARENT_WINDOW_QUERY") {
2938
createOrReplaceWatcher(
3039
event.data.selector,

0 commit comments

Comments
 (0)