Skip to content

Commit 95e5175

Browse files
harshmandandummdidummjrmajorRich-Harris
authored
fix: do not defer unmount; immediately unmount components (#16624)
* fix: do not defer unmount; immediately unmount components * fix: add changeset * Update .changeset/few-geese-itch.md Co-authored-by: Jeremiasz Major <[email protected]> * add test * update changeset --------- Co-authored-by: Simon H <[email protected]> Co-authored-by: Jeremiasz Major <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 036d458 commit 95e5175

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

.changeset/few-geese-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: destroy dynamic component instance before creating new one

packages/svelte/src/internal/client/dom/blocks/svelte-component.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ export function component(node, get_component, render_fn) {
3434
var pending_effect = null;
3535

3636
function commit() {
37-
if (effect) {
38-
pause_effect(effect);
39-
effect = null;
40-
}
41-
4237
if (offscreen_fragment) {
4338
// remove the anchor
4439
/** @type {Text} */ (offscreen_fragment.lastChild).remove();
@@ -56,6 +51,11 @@ export function component(node, get_component, render_fn) {
5651

5752
var defer = should_defer_append();
5853

54+
if (effect) {
55+
pause_effect(effect);
56+
effect = null;
57+
}
58+
5959
if (component) {
6060
var target = anchor;
6161

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
$effect.pre(() => {
3+
console.log('create A');
4+
return () => console.log('destroy A');
5+
});
6+
</script>
7+
8+
<h1>A</h1>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
$effect.pre(() => {
3+
console.log('create B');
4+
return () => console.log('destroy B');
5+
});
6+
</script>
7+
8+
<h1>B</h1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test } from '../../test';
2+
import { flushSync } from 'svelte';
3+
4+
export default test({
5+
mode: ['client', 'hydrate'],
6+
7+
async test({ assert, target, logs }) {
8+
const [button] = target.querySelectorAll('button');
9+
10+
flushSync(() => button.click());
11+
assert.deepEqual(logs, ['create A', 'destroy A', 'create B']);
12+
}
13+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import A from './A.svelte';
3+
import B from './B.svelte';
4+
5+
let condition = $state(true);
6+
let Component = $derived(condition ? A : B);
7+
</script>
8+
9+
<button onclick={() => condition = !condition}>
10+
toggle ({condition})
11+
</button>
12+
13+
<Component />

0 commit comments

Comments
 (0)