Skip to content

Commit 6076bab

Browse files
authored
fix: issue state_proxy_unmount warning when unmounting a state proxy (#16747)
* fix: issue `state_proxy_unmount` warning when unmounting a state proxy * regenerate
1 parent 6798efa commit 6076bab

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
import { boundary } from './dom/blocks/boundary.js';
3535

3636
/**
@@ -316,7 +316,11 @@ export function unmount(component, options) {
316316
}
317317

318318
if (DEV) {
319-
w.lifecycle_double_unmount();
319+
if (STATE_SYMBOL in component) {
320+
w.state_proxy_unmount();
321+
} else {
322+
w.lifecycle_double_unmount();
323+
}
320324
}
321325

322326
return Promise.resolve();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ export function state_proxy_equality_mismatch(operator) {
224224
}
225225
}
226226

227+
/**
228+
* Tried to unmount a state proxy, rather than a component
229+
*/
230+
export function state_proxy_unmount() {
231+
if (DEV) {
232+
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);
233+
} else {
234+
console.warn(`https://svelte.dev/e/state_proxy_unmount`);
235+
}
236+
}
237+
227238
/**
228239
* A `<svelte:boundary>` `reset` function only resets the boundary the first time it is called
229240
*/

0 commit comments

Comments
 (0)