diff --git a/documentation/docs/02-runes/04-$effect.md b/documentation/docs/02-runes/04-$effect.md index 5820e178a0..4a27a72778 100644 --- a/documentation/docs/02-runes/04-$effect.md +++ b/documentation/docs/02-runes/04-$effect.md @@ -255,6 +255,30 @@ const destroy = $effect.root(() => { destroy(); ``` +## `$effect.active` + +The `$effect.active` 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`. + +```svelte + + + +``` + ## When not to use `$effect` 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... diff --git a/packages/svelte/src/internal/client/reactivity/effects.js b/packages/svelte/src/internal/client/reactivity/effects.js index 7921b1e5e9..75f8aa22f3 100644 --- a/packages/svelte/src/internal/client/reactivity/effects.js +++ b/packages/svelte/src/internal/client/reactivity/effects.js @@ -48,6 +48,8 @@ import { Batch, schedule_effect } from './batch.js'; import { flatten } from './async.js'; /** + * If an effect can be created in the current context, `VALID_EFFECT_PARENT` is returned. + * If not, a value indicating why is returned. * @returns {number} */ function active_root_effect() {