Skip to content

Commit 7253f49

Browse files
authored
Merge pull request #3 from observablehq/mythmon/double-render
Re-arrange useEffect to reduce race condition
2 parents 31e26e9 + e9c4ec7 commit 7253f49

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ const ref = useRef<HTMLDivElement | null>(null);
3737

3838
useEffect(() => {
3939
(async () => {
40-
const target = ref.current;
41-
if (!target) return;
42-
while (target.firstChild) target.removeChild(target.firstChild);
4340
const mod = await import(/* webpackIgnore: true */ module);
4441
const component = mod[importName];
4542
const element = component instanceof Function ? await component() : component;
43+
44+
const target = ref.current;
45+
if (!target) return;
46+
while (target.firstChild) target.removeChild(target.firstChild);
4647
target.append(element);
4748
})();
4849
}, [importName, module]);

src/components/observableEmbed/client.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ export const ObservableEmbedClient: React.FC<ObservableEmbedProps> = ({
1414

1515
useEffect(() => {
1616
(async () => {
17-
const target = ref.current;
18-
if (!target) return;
19-
while (target.firstChild) target.removeChild(target.firstChild);
2017
const mod = await import(/* webpackIgnore: true */ module);
2118
const component = mod[importName];
2219
const element = component instanceof Function ? await component() : component;
20+
21+
const target = ref.current;
22+
if (!target) return;
23+
while (target.firstChild) target.removeChild(target.firstChild);
2324
target.append(element);
2425
})();
2526
}, [importName, module]);

0 commit comments

Comments
 (0)