Skip to content

Commit 6ad27e1

Browse files
committed
update example
1 parent d1dc1d6 commit 6ad27e1

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

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

+22-8
Original file line numberDiff line numberDiff line change
@@ -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 `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#H4sIAAAAAAAAE21RQW6DMBD8ytaNBJHaJJdeHECq-oyQgwPryqoxFl5oIsTfa5sQeggSAs_Ojmd2R2ZEg4yzr9bUilRrHCgDKCVW5Ngbk0qjY_w0MrrZQAyAx-9tn9bu3ICaAnYRDp_hVWsIjZfjLHNVpywVpSlJNbbtCHxVIpEC2bUNJJUwg3DvC5ocAzW8GiN3dgk5bBwJwpS6HrfHlaDbbi0mr1Ie_JNEhuds5mRpuoW8gDFqk5KQPpS3C1rS4iEdZ13H4RR_zjDNV5Y0AWqHT3oehPCJ9Gy_hjdZrYY4hezSE_lAram0qn7ycbb2P-nL4zAVs-haVQ7GtRwF97NisQwuU8b2BGF_ecligJLBRZmaD0L3mI8Rm2BfBJN3YyZzdNMYVTy0JKyVs1rcOEiN13vEb2E5HHYfHTYRmWLW2O23T3glxsOaprM_CaV__dWMS-EHN_0BitaP1YECAAA=).
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-
updateCanvas('running');
155+
import confetti from 'canvas-confetti';
156+
157+
let condition = $state(true);
158+
let color = $state('#ff3e00');
148159

149-
if (a) {
150-
updateCanvas('b:', b);
160+
$effect(() => {
161+
if (condition) {
162+
confetti({ colors: [color] });
163+
} else {
164+
confetti();
151165
}
152166
});
153167
```

0 commit comments

Comments
 (0)