Skip to content

Commit ce382fa

Browse files
committed
clean up example, add comments
1 parent 2a03843 commit ce382fa

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/App.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import React, { useState, useEffect } from "react";
33

44
const App = () => {
55
const sample = {
6-
name: "value",
7-
example2: 19999,
8-
example3: { hello: "world" },
6+
example1: "",
7+
example2: false,
98
input: "",
109
};
1110

@@ -25,27 +24,31 @@ const App = () => {
2524
const [data, setData] = useState(sample);
2625
return (
2726
<div>
27+
<h1> React-Retool</h1>
2828
<button
2929
onClick={() => {
30-
setData({ ...data, example2: "new value" });
30+
setData({ ...data, example2: !data.example2 });
3131
}}
3232
>
33-
This is button
33+
Click me!
3434
</button>
35+
<br />
36+
<br />
37+
<label> Share something: </label>
3538
<input
3639
type="text"
3740
value={data.input}
3841
onChange={(e) => setData({ ...data, input: e.target.value })}
3942
/>
40-
<p id="hello"> Hey123 </p>
41-
<h1> {retoolData} </h1>
42-
<p id="hello"> {JSON.stringify(data)} </p>
43+
<br />
44+
<br />
4345
<Retool
4446
url="https://support.retool.com/embedded/public/cb9e15f0-5d7c-43a7-a746-cdec870dde9a"
4547
data={data}
46-
height="500px"
47-
width="500px"
48+
height="700px"
49+
width="1000px"
4850
></Retool>
51+
<h1> {retoolData} </h1>
4952
</div>
5053
);
5154
};

src/components/Retool.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Retool = ({ data, url, height, width }) => {
44
const embeddedIframe = useRef(null);
55
const [elementWatchers, setElementWatchers] = useState({});
66

7+
/* Retool passes up the list of elements to watch on page load */
78
useEffect(() => {
89
for (const key in elementWatchers) {
910
const watcher = elementWatchers[key];
@@ -19,7 +20,9 @@ const Retool = ({ data, url, height, width }) => {
1920
}
2021
}, [data, elementWatchers]);
2122

23+
/* On page load, add event listener to listen for events from Retool */
2224
useEffect(() => {
25+
/* Handle events - if PWQ then create/replace watchers -> return result */
2326
const handler = (event) => {
2427
if (!embeddedIframe?.current?.contentWindow) return;
2528
if (event.data.type === "PARENT_WINDOW_QUERY") {
@@ -37,6 +40,7 @@ const Retool = ({ data, url, height, width }) => {
3740
return () => window.removeEventListener("message", handler);
3841
}, []);
3942

43+
/* Creates or updates the list of values for us to watch for changes */
4044
const createOrReplaceWatcher = (selector, pageName, queryId) => {
4145
const watcherId = pageName + "-" + queryId;
4246
const updatedState = elementWatchers;
@@ -51,6 +55,7 @@ const Retool = ({ data, url, height, width }) => {
5155
setElementWatchers(updatedState);
5256
};
5357

58+
/* Checks for selectors for data and posts message for Retool to read */
5459
const postMessageForSelector = (messageType, eventData) => {
5560
const maybeData = data[eventData.selector];
5661

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React from "react"
2-
import ReactDOM from "react-dom"
3-
import "./index.css"
4-
import App from "./App"
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import "./index.css";
4+
import App from "./App";
55

66
ReactDOM.render(
77
<React.StrictMode>
88
<App />
99
</React.StrictMode>,
1010
document.getElementById("root")
11-
)
11+
);

0 commit comments

Comments
 (0)