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 <jrh.mjr@gmail.com>

* add test

* update changeset

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Jeremiasz Major <jrh.mjr@gmail.com>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/16633/head
Harsh Mandan 1 month ago committed by GitHub
parent 036d4588ab
commit 95e5175581
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: destroy dynamic component instance before creating new one

@ -34,11 +34,6 @@ export function component(node, get_component, render_fn) {
var pending_effect = null; var pending_effect = null;
function commit() { function commit() {
if (effect) {
pause_effect(effect);
effect = null;
}
if (offscreen_fragment) { if (offscreen_fragment) {
// remove the anchor // remove the anchor
/** @type {Text} */ (offscreen_fragment.lastChild).remove(); /** @type {Text} */ (offscreen_fragment.lastChild).remove();
@ -56,6 +51,11 @@ export function component(node, get_component, render_fn) {
var defer = should_defer_append(); var defer = should_defer_append();
if (effect) {
pause_effect(effect);
effect = null;
}
if (component) { if (component) {
var target = anchor; var target = anchor;

@ -0,0 +1,8 @@
<script>
$effect.pre(() => {
console.log('create A');
return () => console.log('destroy A');
});
</script>
<h1>A</h1>

@ -0,0 +1,8 @@
<script>
$effect.pre(() => {
console.log('create B');
return () => console.log('destroy B');
});
</script>
<h1>B</h1>

@ -0,0 +1,13 @@
import { test } from '../../test';
import { flushSync } from 'svelte';
export default test({
mode: ['client', 'hydrate'],
async test({ assert, target, logs }) {
const [button] = target.querySelectorAll('button');
flushSync(() => button.click());
assert.deepEqual(logs, ['create A', 'destroy A', 'create B']);
}
});

@ -0,0 +1,13 @@
<script>
import A from './A.svelte';
import B from './B.svelte';
let condition = $state(true);
let Component = $derived(condition ? A : B);
</script>
<button onclick={() => condition = !condition}>
toggle ({condition})
</button>
<Component />
Loading…
Cancel
Save