Skip to content

Commit f40efb2

Browse files
authored
$effect.active docs (#9598)
* docs: $effect.active * link --------- Co-authored-by: Rich Harris <[email protected]>
1 parent c837771 commit f40efb2

File tree

1 file changed

+18
-0
lines changed
  • sites/svelte-5-preview/src/routes/docs/content/01-api

1 file changed

+18
-0
lines changed

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

+18
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ In rare cases, you may need to run code _before_ the DOM updates. For this we ca
168168

169169
Previously, you would have used `beforeUpdate`, which — like `afterUpdate` — is deprecated in Svelte 5.
170170

171+
## `$effect.active`
172+
173+
The `$effect.active` rune is an advanced feature that tells you whether or not the code is running inside an effect or inside your template ([demo](/#H4sIAAAAAAAAE3XP0QrCMAwF0F-JRXAD595rLfgdzodRUyl0bVgzQcb-3VYFQfExl5tDMgvrPCYhT7MI_YBCiiOR2Aq-UxnSDT1jnlOcRlMSlczoiHUXOjYxpOhx5-O12rgAJg4UAwaGhDyR3Gxhjdai4V1v2N2wqus9tC3Y3ifMQjbehaqq4aBhLtEv_Or893icCsdLve-Caj8nBkU67zMO5HtGCfM3sKiWNKhV0zwVaBqd3x3ixVmHFyFLuJyXB-moOe8pAQAA)):
174+
175+
```svelte
176+
<script>
177+
console.log('in component setup:', $effect.active()); // false
178+
179+
$effect(() => {
180+
console.log('in effect:', $effect.active()); // true
181+
});
182+
</script>
183+
184+
<p>in template: {$effect.active()}</p> <!-- true -->
185+
```
186+
187+
This allows you to (for example) add things like subscriptions without causing memory leaks, by putting them in child effects.
188+
171189
## `$props`
172190

173191
To declare component props, use the `$props` rune:

0 commit comments

Comments
 (0)