You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md
+14-7Lines changed: 14 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -270,31 +270,38 @@ You can return a function from `$effect`, which will run immediately before the
270
270
If you update `$state` inside an `$effect`, you most likely want to use `$derived` instead.
271
271
272
272
```svelte
273
+
<!-- Don't do this -->
273
274
<script>
274
275
let count = $state(0);
275
-
// Don't do this:
276
276
let doubled = $state();
277
277
$effect(() => {
278
278
doubled = count * 2;
279
279
});
280
-
// Do this instead:
280
+
</script>
281
+
282
+
<!-- Do this instead: -->
283
+
<script>
284
+
let count = $state(0);
281
285
let doubled = $derived(count * 2);
282
286
</script>
283
287
```
284
288
285
289
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`.
286
290
287
291
```svelte
292
+
<!-- Don't do this -->
288
293
<script>
289
-
// Don't do this:
290
294
let result_1 = $state();
291
295
let result_2 = $state();
292
296
$effect(() => {
293
297
// ... some lengthy code resulting in
294
298
result_1 = someValue;
295
299
result_2 = someOtherValue;
296
300
});
297
-
// Do this instead:
301
+
</script>
302
+
303
+
<!-- Do this instead: -->
304
+
<script>
298
305
let { result_1, result_2 } = $derived.by(() => {
299
306
// ... some lengthy code resulting in
300
307
return {
@@ -305,7 +312,7 @@ This also applies to more complex calculations that require more than a simple e
305
312
</script>
306
313
```
307
314
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.
309
316
310
317
```svelte
311
318
<!-- Don't do this -->
@@ -337,7 +344,7 @@ If you want to have something update from above but also modify it from below (i
337
344
```svelte
338
345
<script>
339
346
let { value } = $props();
340
-
let proxy = {
347
+
let facade = {
341
348
get value() {
342
349
return value.toUpperCase();
343
350
},
@@ -347,7 +354,7 @@ If you want to have something update from above but also modify it from below (i
347
354
};
348
355
</script>
349
356
350
-
<input bind:value={proxy.value} />
357
+
<input bind:value={facade.value} />
351
358
```
352
359
353
360
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