Skip to content

Commit 6a01f48

Browse files
authored
docs: clarify effect alternatives section (#10718)
A handful of minor cleanups: - there were two different "do this" / "don't do this" formats. Standardize on the one that's much easier to read - clarify that callback props are being used (component events no longer exist in Svelte 5) - rename variable from `proxy` to `facade` since it's not a proxy
1 parent 881e84f commit 6a01f48

File tree

1 file changed

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

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,31 +270,38 @@ You can return a function from `$effect`, which will run immediately before the
270270
If you update `$state` inside an `$effect`, you most likely want to use `$derived` instead.
271271

272272
```svelte
273+
<!-- Don't do this -->
273274
<script>
274275
let count = $state(0);
275-
// Don't do this:
276276
let doubled = $state();
277277
$effect(() => {
278278
doubled = count * 2;
279279
});
280-
// Do this instead:
280+
</script>
281+
282+
<!-- Do this instead: -->
283+
<script>
284+
let count = $state(0);
281285
let doubled = $derived(count * 2);
282286
</script>
283287
```
284288

285289
This also applies to more complex calculations that require more than a simple expression and write to more than one variable. In these cases, you can use `$derived.by`.
286290

287291
```svelte
292+
<!-- Don't do this -->
288293
<script>
289-
// Don't do this:
290294
let result_1 = $state();
291295
let result_2 = $state();
292296
$effect(() => {
293297
// ... some lengthy code resulting in
294298
result_1 = someValue;
295299
result_2 = someOtherValue;
296300
});
297-
// Do this instead:
301+
</script>
302+
303+
<!-- Do this instead: -->
304+
<script>
298305
let { result_1, result_2 } = $derived.by(() => {
299306
// ... some lengthy code resulting in
300307
return {
@@ -305,7 +312,7 @@ This also applies to more complex calculations that require more than a simple e
305312
</script>
306313
```
307314

308-
When reacting to a state change and writing to a different state as a result, think about if it's possible to model the code through event handling instead.
315+
When reacting to a state change and writing to a different state as a result, think about if it's possible to use callback props instead.
309316

310317
```svelte
311318
<!-- Don't do this -->
@@ -337,7 +344,7 @@ If you want to have something update from above but also modify it from below (i
337344
```svelte
338345
<script>
339346
let { value } = $props();
340-
let proxy = {
347+
let facade = {
341348
get value() {
342349
return value.toUpperCase();
343350
},
@@ -347,7 +354,7 @@ If you want to have something update from above but also modify it from below (i
347354
};
348355
</script>
349356
350-
<input bind:value={proxy.value} />
357+
<input bind:value={facade.value} />
351358
```
352359

353360
If you absolutely have to update `$state` within an effect and run into an infinite loop because you read and write to the same `$state`, use [untrack](functions#untrack).

0 commit comments

Comments
 (0)