Skip to content

Commit 68b8d88

Browse files
committed
use onComponentsLoad
1 parent 1c6520c commit 68b8d88

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/react-test.html

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,42 @@
1818
<script type="text/babel">
1919
'use strict';
2020

21-
function Embeddable(props: { token: string }) {
22-
return React.createElement('em-beddable', props);
21+
function Embeddable({onComponentsLoad, ...props}) {
22+
const ref = React.useRef(null);
23+
24+
React.useEffect(() => {
25+
ref.current.addEventListener('componentsLoad', onComponentsLoad);
26+
return () => {
27+
ref.current.removeEventListener('componentsLoad', onComponentsLoad);
28+
};
29+
}, []);
30+
return React.createElement('em-beddable', {...props, ref});
2331
}
2432

25-
class TestComponent extends React.Component {
26-
constructor(props) {
27-
super(props);
28-
this.state = { load: false };
29-
}
33+
const TestComponent = () => {
34+
const [load, setLoad] = React.useState(false);
35+
const [isLoading, setIsLoading] = React.useState(false);
3036

31-
render() {
32-
if (this.state.load) {
33-
return <Embeddable
34-
token="eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiJmZWE5NDA0Ny1lMjhhLTQ5ZGQtOGQ0YS1mZDdjNDY2MDE0YzUiLCJpYXQiOjE3MDc3Njk3ODEsImV4cCI6MTcwODM3NDU4MX0.IglDlXs9JymBwLrpPUMREpSkClkjY0xuJmoXgHwiqLk"
35-
/>;
37+
if (load) {
38+
return (
39+
<div>
40+
{isLoading && <div>Loading dashboard...</div>}
41+
<Embeddable
42+
token="eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiJlNWQ2ZWFmZS0yMDMzLTRkYjgtYTdiYy1mYTAyOTkzYjczZGQiLCJpYXQiOjE3MDg1OTA5MDgsImV4cCI6MTcwOTE5NTcwOH0.ccj1bNbT3KOmYCu7Oi7JWT7Yea1vo0Q5xN30mvA9i64"
43+
onComponentsLoad={() => setIsLoading(false)}
44+
/>
45+
</div>
46+
);
3647
}
3748

3849
return (
39-
<button onClick={() => this.setState({ load: true })}>
50+
<button onClick={() => {
51+
setIsLoading(true);
52+
setLoad(true);
53+
}}>
4054
Load my dashboard
4155
</button>
4256
);
43-
}
4457
}
4558

4659
const domContainer = document.querySelector('#test-container');

0 commit comments

Comments
 (0)