-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Open
Labels
good first issueA small, encapsulated issue, ideal for a new contributor to Svelte to tackle.A small, encapsulated issue, ideal for a new contributor to Svelte to tackle.
Description
Describe the bug
In Attachment, mount component programmatically like below.
const attachement: Attachement<HTMLElement> = (element) => {
let componentInstance = $state<ReturnType<typeof mount>>();
const target = document.createElement('div');
componentInstance = mount(Component, { target }); // if use component instance with $state, cannnot call unmount.
// const componentInstance = mount(Component, { target }); // This is works
return () => {
console.log('hidden attachement');
if (componentInstance) {
// This warning appears in console on calling this.
//
// [svelte] lifecycle_double_unmount
// Tried to unmount a component that was not mounted
// https://svelte.dev/e/lifecycle_double_unmount
//
void unmount(componentInstance);
}
}
}
Then use this like below
<button onclick={() => visible = !visible}>Toggle `hello`</button>
{#if visible}
<h1 {@attach attachement}>hello</h1>
{/if}
If visible changes to false
, attachement teardown is called.
But unmount(componentInstance
is not works.
And this warning is appear.
[svelte] lifecycle_double_unmount
Tried to unmount a component that was not mounted
https://svelte.dev/e/lifecycle_double_unmount
Workaround
If use componentInstance
without $state
, it works.
// NG
let componentInstance = $state<ReturnType<typeof mount>>();
const target = document.createElement('div');
componentInstance = mount(Component, { target });
// OK
const target = document.createElement('div');
const componentInstance = mount(Component, { target });
Reproduction
Logs
System Info
System:
OS: macOS 15.6.1
CPU: (10) arm64 Apple M2 Pro
Memory: 68.94 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.3.0 - ~/.nodenv/versions/24.3.0/bin/node
npm: 11.4.2 - ~/.nodenv/versions/24.3.0/bin/npm
pnpm: 10.14.0 - ~/.nodenv/versions/24.3.0/bin/pnpm
Browsers:
Chrome: 139.0.7258.139
Safari: 18.6
Severity
annoyance
Metadata
Metadata
Assignees
Labels
good first issueA small, encapsulated issue, ideal for a new contributor to Svelte to tackle.A small, encapsulated issue, ideal for a new contributor to Svelte to tackle.