Skip to content

Commit 93b70a0

Browse files
committed
load import source in effect
1 parent 3f8803e commit 93b70a0

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

docs/source/about/changelog.rst

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@ scheme for the project adheres to `Semantic Versioning <https://semver.org/>`__.
1616
Unreleased
1717
----------
1818

19-
While this release doesn't warrant a minor version bump, the last release should have
20-
been. Thus, to make up for that, this release will be a minor bump. It includes misc
21-
bug fixes, and several feature adds, the most important of which is the addition of the
22-
``use_context`` hook.
23-
2419
Added:
2520

2621
- Support for keys in HTML fragments - :issue:`682`
22+
- Use Context Hook - :pull:`585`
2723

28-
**Merged Pull Requests**
24+
Fixed:
2925

30-
- reset schedule_render_later flag after triggering - :pull:`688`
31-
- support keys in HTML fragments - :pull:`683`
32-
- Add Use Context Hook - :pull:`585`
26+
- React warning about set state in unmounted component - :issue:`690`
27+
- Missing reset of schedule_render_later flag - :pull:`688`
3328

3429
----
3530

src/client/packages/idom-client-react/src/components.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ReactDOM from "react-dom";
33
import htm from "htm";
44

55
import { useJsonPatchCallback } from "./json-patch.js";
6-
import { loadModelImportSource } from "./import-source.js";
6+
import { useImportSource } from "./import-source.js";
77
import {
88
createElementAttributes,
99
createElementChildren,
@@ -101,14 +101,9 @@ function ImportedElement({ model }) {
101101
const layoutContext = React.useContext(LayoutContext);
102102

103103
const importSourceFallback = model.importSource.fallback;
104-
const [importSource, setImportSource] = React.useState(null);
104+
const importSource = useImportSource(model.importSource);
105105

106106
if (!importSource) {
107-
// load the import source in the background
108-
loadModelImportSource(layoutContext, model.importSource).then(
109-
setImportSource
110-
);
111-
112107
// display a fallback if one was given
113108
if (!importSourceFallback) {
114109
return html`<div />`;

src/client/packages/idom-client-react/src/import-source.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1+
import React from "react";
2+
13
import {
24
createElementAttributes,
35
createElementChildren,
46
} from "./element-utils.js";
57

6-
export function loadModelImportSource(layoutContext, importSource) {
8+
export function useImportSource(modelImportSource) {
9+
const layoutContext = React.useContext(LayoutContext);
10+
const [importSource, setImportSource] = React.useState(null);
11+
12+
useEffect(() => {
13+
let unmounted = false;
14+
15+
loadModelImportSource(layoutContext, modelImportSource).then((src) => {
16+
if (!unmounted) {
17+
setImportSource(src);
18+
}
19+
});
20+
21+
return () => {
22+
unmounted = true;
23+
};
24+
}, [layoutContext, modelImportSource, setImportSource]);
25+
26+
return importSource;
27+
}
28+
29+
function loadModelImportSource(layoutContext, importSource) {
730
return layoutContext
831
.loadImportSource(importSource.source, importSource.sourceType)
932
.then((module) => {

0 commit comments

Comments
 (0)