Skip to content

Commit 0ff3d74

Browse files
docs: update $effect examples (#15463)
* docs: update effect examples * revert * Update documentation/docs/02-runes/04-$effect.md * update example * revert * update effect root example --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 0ca1f4a commit 0ff3d74

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

documentation/docs/02-runes/04-$effect.md

+34-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can create an effect with the `$effect` rune ([demo](/playground/untitled#H4
2525
});
2626
</script>
2727
28-
<canvas bind:this={canvas} width="100" height="100" />
28+
<canvas bind:this={canvas} width="100" height="100"></canvas>
2929
```
3030

3131
When Svelte runs an effect function, it tracks which pieces of state (and derived state) are accessed (unless accessed inside [`untrack`](svelte#untrack)), and re-runs the function when that state later changes.
@@ -135,19 +135,33 @@ An effect only reruns when the object it reads changes, not when a property insi
135135

136136
An effect only depends on the values that it read the last time it ran. This has interesting implications for effects that have conditional code.
137137

138-
For instance, if `a` is `true` in the code snippet below, the code inside the `if` block will run and `b` will be evaluated. As such, changes to either `a` or `b` [will cause the effect to re-run](/playground/untitled#H4sIAAAAAAAAE3VQzWrDMAx-FdUU4kBp71li6EPstOxge0ox8-QQK2PD-N1nLy2F0Z2Evj9_chKkP1B04pnYscc3cRCT8xhF95IEf8-Vq0DBr8rzPB_jJ3qumNERH-E2ECNxiRF9tIubWY00lgcYNAywj6wZJS8rtk83wjwgCrXHaULLUrYwKEgVGrnkx-Dx6MNFNstK5OjSbFGbwE0gdXuT_zGYrjmAuco515Hr1p_uXak3K3MgCGS9s-9D2grU-judlQYXIencnzad-tdR79qZrMyvw9wd5Z8Yv1h09dz8mn8AkM7Pfo0BAAA=).
138+
For instance, if `condition` is `true` in the code snippet below, the code inside the `if` block will run and `color` will be evaluated. As such, changes to either `condition` or `color` [will cause the effect to re-run](/playground/untitled#H4sIAAAAAAAAE21RQW6DMBD8ytaNBJHaJFLViwNIVZ8RcnBgXVk1xsILTYT4e20TQg89IOPZ2fHM7siMaJBx9tmaWpFqjQNlAKXEihx7YVJpdIyfRkY3G4gB8Pi97cPanRtQU8AuwuF_eNUaQuPlOMtc1SlLRWlKUo1tOwJflUikQHZtA0klzCDc64Imx0ANn8bInV1CDhtHgjClrsftcSXotluLybOUb3g4JJHhOZs5WZpuIS9gjNqkJKQP5e2ClrR4SMdZ13E4xZ8zTPOTJU2A2uE_PQ9COCI926_hTVarIU4hu_REPlBrKq2q73ycrf1N-vS4TMUsulaVg3EtR8H9rFgsg8uUsT1B2F9eshigZHBRpuaD0D3mY8Qm2BfB5N2YyRzdNEYVDy0Ja-WsFjcOUuP1HvFLWA6H3XuHTUSmmDV2--0TXonxsKbp7G9C6R__NONS-MFNvxj_d6mBAgAA).
139139

140-
Conversely, if `a` is `false`, `b` will not be evaluated, and the effect will _only_ re-run when `a` changes.
140+
Conversely, if `condition` is `false`, `color` will not be evaluated, and the effect will _only_ re-run again when `condition` changes.
141141

142142
```ts
143-
let a = false;
144-
let b = false;
143+
// @filename: ambient.d.ts
144+
declare module 'canvas-confetti' {
145+
interface ConfettiOptions {
146+
colors: string[];
147+
}
148+
149+
function confetti(opts?: ConfettiOptions): void;
150+
export default confetti;
151+
}
152+
153+
// @filename: index.js
145154
// ---cut---
146-
$effect(() => {
147-
console.log('running');
155+
import confetti from 'canvas-confetti';
148156

149-
if (a) {
150-
console.log('b:', b);
157+
let condition = $state(true);
158+
let color = $state('#ff3e00');
159+
160+
$effect(() => {
161+
if (condition) {
162+
confetti({ colors: [color] });
163+
} else {
164+
confetti();
151165
}
152166
});
153167
```
@@ -211,20 +225,19 @@ It is used to implement abstractions like [`createSubscriber`](/docs/svelte/svel
211225

212226
The `$effect.root` rune is an advanced feature that creates a non-tracked scope that doesn't auto-cleanup. This is useful for nested effects that you want to manually control. This rune also allows for the creation of effects outside of the component initialisation phase.
213227

214-
```svelte
215-
<script>
216-
let count = $state(0);
228+
```js
229+
const destroy = $effect.root(() => {
230+
$effect(() => {
231+
// setup
232+
});
217233

218-
const cleanup = $effect.root(() => {
219-
$effect(() => {
220-
console.log(count);
221-
});
234+
return () => {
235+
// cleanup
236+
};
237+
});
222238

223-
return () => {
224-
console.log('effect root cleanup');
225-
};
226-
});
227-
</script>
239+
// later...
240+
destroy();
228241
```
229242

230243
## When not to use `$effect`

0 commit comments

Comments
 (0)