Skip to content

Commit b8c6b6c

Browse files
committed
fix: issue state_proxy_unmount warning when unmounting a state proxy
1 parent f09f25e commit b8c6b6c

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

.changeset/fuzzy-pillows-tell.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: issue `state_proxy_unmount` warning when unmounting a state proxy

documentation/docs/98-reference/.generated/client-warnings.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,27 @@ Reactive `$state(...)` proxies and the values they proxy have different identiti
312312
313313
To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy.
314314
315+
### state_proxy_unmount
316+
317+
```
318+
Tried to unmount a state proxy, rather than a component
319+
```
320+
321+
`unmount` was called with a state proxy:
322+
323+
```js
324+
import { mount, unmount } from 'svelte';
325+
import Component from './Component.svelte';
326+
let target = document.body;
327+
// ---cut---
328+
let component = $state(mount(Component, { target }));
329+
330+
// later...
331+
unmount(component);
332+
```
333+
334+
Avoid using `$state` here. If `component` _does_ need to be reactive for some reason, use `$state.raw` instead.
335+
315336
### svelte_boundary_reset_noop
316337
317338
```

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ To silence the warning, ensure that `value`:
272272
273273
To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy.
274274
275+
## state_proxy_unmount
276+
277+
> Tried to unmount a state proxy, rather than a component
278+
279+
`unmount` was called with a state proxy:
280+
281+
```js
282+
import { mount, unmount } from 'svelte';
283+
import Component from './Component.svelte';
284+
let target = document.body;
285+
// ---cut---
286+
let component = $state(mount(Component, { target }));
287+
288+
// later...
289+
unmount(component);
290+
```
291+
292+
Avoid using `$state` here. If `component` _does_ need to be reactive for some reason, use `$state.raw` instead.
293+
275294
## svelte_boundary_reset_noop
276295
277296
> A `<svelte:boundary>` `reset` function only resets the boundary the first time it is called

packages/svelte/src/internal/client/render.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import * as w from './warnings.js';
3030
import * as e from './errors.js';
3131
import { assign_nodes } from './dom/template.js';
3232
import { is_passive_event } from '../../utils.js';
33-
import { COMMENT_NODE } from './constants.js';
33+
import { COMMENT_NODE, STATE_SYMBOL } from './constants.js';
3434

3535
/**
3636
* This is normally true — block effects should run their intro transitions —
@@ -309,7 +309,11 @@ export function unmount(component, options) {
309309
}
310310

311311
if (DEV) {
312-
w.lifecycle_double_unmount();
312+
if (STATE_SYMBOL in component) {
313+
w.state_proxy_unmount();
314+
} else {
315+
w.lifecycle_double_unmount();
316+
}
313317
}
314318

315319
return Promise.resolve();

packages/svelte/src/internal/client/warnings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ export function lifecycle_double_unmount() {
171171
}
172172
}
173173

174+
/**
175+
* Tried to unmount a state proxy, rather than a component
176+
*/
177+
export function state_proxy_unmount() {
178+
if (DEV) {
179+
console.warn(`%c[svelte] state_proxy_unmount\n%cTried to unmount a state proxy, rather than a component\nhttps://svelte.dev/e/state_proxy_unmount`, bold, normal);
180+
} else {
181+
console.warn(`https://svelte.dev/e/state_proxy_unmount`);
182+
}
183+
}
184+
174185
/**
175186
* %parent% passed property `%prop%` to %child% with `bind:`, but its parent component %owner% did not declare `%prop%` as a binding. Consider creating a binding between %owner% and %parent% (e.g. `bind:%prop%={...}` instead of `%prop%={...}`)
176187
* @param {string} parent

0 commit comments

Comments
 (0)