Skip to content

Commit fe95534

Browse files
Fix custom component reload mode (#12968)
* Fix custom component reload mode * add changeset --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent 4a0fe6e commit fe95534

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

.changeset/ready-symbols-end.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/core": patch
3+
"gradio": patch
4+
---
5+
6+
fix:Fix custom component reload mode

js/core/src/MountComponents.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import MountCustomComponent from "./MountCustomComponent.svelte";
44
let { node, ...rest } = $props();
55
6-
$inspect(node);
7-
86
let component = $derived(await node.component);
97
</script>
108

js/core/src/MountCustomComponent.svelte

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
<script lang="ts">
2-
import { onMount, onDestroy } from "svelte";
32
let { node, children, ...rest } = $props();
43
54
let component = $derived(await node.component);
65
let runtime = $derived(
76
(await node.runtime) as {
87
mount: typeof import("svelte").mount;
9-
umount: typeof import("svelte").unmount;
8+
unmount: typeof import("svelte").unmount;
109
}
1110
);
1211
let el: HTMLElement = $state(null);
13-
let comp;
1412
1513
$effect(() => {
16-
if (el && !comp && runtime) {
17-
comp = runtime.mount(component.default, {
18-
target: el,
14+
if (!el || !runtime || !component) return;
1915
20-
props: {
21-
shared_props: node.props.shared_props,
22-
props: node.props.props,
23-
children
24-
}
25-
});
26-
}
27-
});
16+
// Read prop references so the effect re-runs when the node is
17+
// replaced during a dev reload (new objects are created by
18+
// app_tree.reload).
19+
const _shared_props = node.props.shared_props;
20+
const _props = node.props.props;
21+
const _runtime = runtime;
2822
29-
onDestroy(() => {
30-
if (comp) {
31-
runtime.umount(comp);
32-
}
23+
const mounted = _runtime.mount(component.default, {
24+
target: el,
25+
props: {
26+
shared_props: _shared_props,
27+
props: _props,
28+
children
29+
}
30+
});
31+
32+
return () => {
33+
_runtime.unmount(mounted);
34+
};
3335
});
3436
</script>
3537

0 commit comments

Comments
 (0)