Skip to content

Commit d74b881

Browse files
sync docs
1 parent 0b335d6 commit d74b881

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

apps/svelte.dev/content/docs/svelte/02-runes/04-$effect.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,30 @@ const destroy = $effect.root(() => {
256256
destroy();
257257
```
258258

259+
## `$effect.allowed`
260+
261+
The `$effect.allowed` rune is an advanced feature that indicates whether or not an effect or [async `$derived`](await-expressions) can be created in the current context. To improve performance and memory efficiency, effects and async deriveds can only be created when a root effect is active. Root effects are created during component setup, but they can also be programmatically created via `$effect.root`.
262+
263+
```svelte
264+
<script>
265+
console.log('in component setup', $effect.allowed()); // true
266+
267+
function onclick() {
268+
console.log('after component setup', $effect.allowed()); // false
269+
}
270+
function ondblclick() {
271+
$effect.root(() => {
272+
console.log('in root effect', $effect.allowed()); // true
273+
return () => {
274+
console.log('in effect teardown', $effect.allowed()); // false
275+
}
276+
})();
277+
}
278+
</script>
279+
<button {onclick}>Click me!</button>
280+
<button {ondblclick}>Click me twice!</button>
281+
```
282+
259283
## When not to use `$effect`
260284

261285
In general, `$effect` is best considered something of an escape hatch — useful for things like analytics and direct DOM manipulation — rather than a tool you should use frequently. In particular, avoid using it to synchronise state. Instead of this...

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy