Skip to content

Commit 56621a2

Browse files
authored
docs: add legacy callouts (#13428)
1 parent d87bf17 commit 56621a2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

documentation/docs/01-introduction/03-reactivity-fundamentals.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ class Todo {
4040
}
4141
```
4242

43+
> [!LEGACY]
44+
> In Svelte 4, state was implicitly reactive if the variable was declared at the top level
45+
>
46+
> ```svelte
47+
> <script>
48+
> let count = 0;
49+
> </script>
50+
>
51+
> <button on:click={() => count++}>
52+
> clicks: {count}
53+
> </button>
54+
> ```
55+
4356
## `$derived`
4457
4558
Derived state is declared with the `$derived` rune:
@@ -61,6 +74,24 @@ The expression inside `$derived(...)` should be free of side-effects. Svelte wil
6174

6275
As with `$state`, you can mark class fields as `$derived`.
6376

77+
> [!LEGACY]
78+
> In Svelte 4, you could use reactive statements for this.
79+
>
80+
> ```svelte
81+
> <script>
82+
> let count = 0;
83+
> $: doubled = count * 2;
84+
> </script>
85+
>
86+
> <button on:click={() => count++}>
87+
> {doubled}
88+
> </button>
89+
>
90+
> <p>{count} doubled is {doubled}</p>
91+
> ```
92+
>
93+
> This only worked at the top level of a component.
94+
6495
## `$effect`
6596
6697
To run _side-effects_ when the component is mounted to the DOM, and when values change, we can use the `$effect` rune ([demo](/#H4sIAAAAAAAAE31T24rbMBD9lUG7kAQ2sbdlX7xOYNk_aB_rQhRpbAsU2UiTW0P-vbrYubSlYGzmzMzROTPymdVKo2PFjzMzfIusYB99z14YnfoQuD1qQh-7bmdFQEonrOppVZmKNBI49QthCc-OOOH0LZ-9jxnR6c7eUpOnuv6KeT5JFdcqbvbcBcgDz1jXKGg6ncFyBedYR6IzLrAZwiN5vtSxaJA-EzadfJEjKw11C6GR22-BLH8B_wxdByWpvUYtqqal2XB6RVkG1CoHB6U1WJzbnYFDiwb3aGEdDa3Bm1oH12sQLTcNPp7r56m_00mHocSG97_zd7ICUXonA5fwKbPbkE2ZtMJGGVkEdctzQi4QzSwr9prnFYNk5hpmqVuqPQjNnfOJoMF22lUsrq_UfIN6lfSVyvQ7grB3X2mjMZYO3XO9w-U5iLx42qg29md3BP_ni5P4gy9ikTBlHxjLzAtPDlyYZmRdjAbGq7HprEQ7p64v4LU_guu0kvAkhBim3nMplWl8FreQD-CW20aZR0wq12t-KqDWeBywhvexKC3memmDwlHAv9q4Vo2ZK8KtK0CgX7u9J8wXbzdKv-nRnfF_2baTqlYoWUF2h5efl9-n0O6koAMAAA==)):
@@ -86,3 +117,28 @@ To run _side-effects_ when the component is mounted to the DOM, and when values
86117
```
87118
88119
The function passed to `$effect` will run when the component mounts, and will re-run after any changes to the values it reads that were declared with `$state` or `$derived` (including those passed in with `$props`). Re-runs are batched (i.e. changing `color` and `size` in the same moment won't cause two separate runs), and happen after any DOM updates have been applied.
120+
121+
> [!LEGACY]
122+
> In Svelte 4, you could use reactive statements for this.
123+
>
124+
> ```svelte
125+
> <script>
126+
> let size = 50;
127+
> let color = '#ff3e00';
128+
>
129+
> let canvas;
130+
>
131+
> $: {
132+
> const context = canvas.getContext('2d');
133+
> context.clearRect(0, 0, canvas.width, canvas.height);
134+
>
135+
> // this will re-run whenever `color` or `size` change
136+
> context.fillStyle = color;
137+
> context.fillRect(0, 0, size, size);
138+
> }
139+
> </script>
140+
>
141+
> <canvas bind:this={canvas} width="100" height="100" />
142+
> ```
143+
>
144+
> This only worked at the top level of a component.

0 commit comments

Comments
 (0)