Skip to content

Commit de59777

Browse files
committed
revert
1 parent c05f5e6 commit de59777

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,32 @@ You can use `$effect` anywhere, not just at the top level of a component, as lon
4040

4141
> [!NOTE] Svelte uses effects internally to represent logic and expressions in your template — this is how `<h1>hello {name}!</h1>` updates when `name` changes.
4242
43-
An effect can return a _teardown function_ which will run immediately before the effect re-runs ([demo](/playground/untitled#H4sIAAAAAAAAA41T24rbQAz9FTEE4oCbuCndsq4dKHltH9ruPnUKmXjkeFhHNjPKrcH_XsaT214ofbOOjs6R5NFRkFqjSMUjseEatYhFaWp0Iv11FHxofc4DIj4zv7Tt2G2xZo8tlcO38KIhRmInUpG5wpqWZ5Ik18jgzB-EHAaOFWN0lySjz-dUoWirnA9JctGQY9i0WjHO-wTkEDHueQT5DI6ec2b1dnuG_CQxXiHPAxYNp3oYLHq2x8ZFjcr-wIKjJIYkPlftjObqElVoVhW_LC0b8j7DD9N2Dw6tKYcvGaauH7yzj2J4n8Tw8TRkF6gkeYBl6f2j18OsURv1fYP2ADnsDOlmN14rLqpvPhEtorXav-tbTWFw9Ovs2v1ocdOoY6gU6Rr7inmlaOVXHuGtmWRTQoRBGt3oiku-3Xq0-FlYRILeEnbKwcqiYrTAlaJrCzEo0sAVEtTo3KUhyV346E5_VvJ1xLHS-qtxjIQ2etW0lwAINRZ5Ywmi5zPcKFlcN1v8t9ilDe58mE2uj5MeGnCIfgAIPycGbdWqB1xYATewRL8IvBn2bRIpa5vd2Atn2mzB8aHGXIq1sitDKUyTdg-JFP1dZMsNc0PQUFGb4ik_hilPp3KXJN2sqLB4grKxPvRPrzfLJqHyP1Tun6vcv62STbTZ9uvIwhnA0pBOuTIuPwakCy8hl-JTkkgB4U5yKaY-nGWTQJuJWPgLECnbDXa_u7-plT6obAQAAA==)).
43+
An effect can return a _teardown function_ which will run immediately before the effect re-runs ([demo](/playground/untitled#H4sIAAAAAAAAE42SQVODMBCF_8pOxkPRKq3HCsx49K4n64xpskjGkDDJ0tph-O8uINo6HjxB3u7HvrehE07WKDbiyZEhi1osRWksRrF57gQdm6E2CKx_dd43zU3co6VB28mIf-nKO0JH_BmRRRVMQ8XWbXkAgfKtI8jhIpIkXKySu7lSG2tNRGZ1_GlYr1ZTD3ddYFmiosUigbyAbpC2lKbwWJkIB8ZhhxBQBWRSw6FCh3sM8GrYTthL-wqqku4N44TyqEgwF3lmRHr4Op0PGXoH31c5rO8mqV-eOZ49bikgtcHBL55tmhIkEMqg_cFB2TpFxjtg703we6NRL8HQFCS07oSUCZi6Rm04lz1yytIHBKoQpo1w6Gsm4gmyS8b8Y5PydeMdX8gwS2Ok4I-ov5NZtvQde95GMsccn_1wzNKfu3RZtS66cSl9lvL7qO1aIk7knbJGvefdtIOzi73M4bYvovUHDFk6AcX_0HRESxnpBOW_jfCDxIZCi_1L_wm4xGQ60wIAAA==)).
4444

4545
```svelte
4646
<script>
47-
let size = $state(600);
48-
let canvas;
47+
let count = $state(0);
48+
let milliseconds = $state(1000);
4949
50-
const updateCanvas = (text) => {
51-
const context = canvas.getContext('2d');
52-
context.clearRect(0, 0, canvas.width, canvas.height);
53-
context.font = '32px serif';
54-
context.fillText(text, 10, 50);
55-
};
56-
5750
$effect(() => {
58-
const mediaQuery = window.matchMedia(`(max-width: ${size}px)`);
59-
const handleMediaChange = (e) => {
60-
if (e.matches) {
61-
updateCanvas(`Screen width was greater than ${size}px, and then less`);
62-
}
63-
};
51+
// This will be recreated whenever `milliseconds` changes
52+
const interval = setInterval(() => {
53+
count += 1;
54+
}, milliseconds);
6455
65-
mediaQuery.addListener(handleMediaChange);
66-
6756
return () => {
68-
mediaQuery.removeListener(handleMediaChange);
57+
// if a teardown function is provided, it will run
58+
// a) immediately before the effect re-runs
59+
// b) when the component is destroyed
60+
clearInterval(interval);
6961
};
7062
});
7163
</script>
7264
73-
To see the effect, drag the screen to be wide, and then drag the screen to be narrow.
74-
75-
<div style="margin: 20px 0">
76-
<button onclick={() => size = 600}>check for 600px screen</button>
77-
<button onclick={() => size = 900}>check for 900px screen</button>
78-
</div>
65+
<h1>{count}</h1>
7966
80-
<canvas bind:this={canvas} width="700" height="200"></canvas>
67+
<button onclick={() => (milliseconds *= 2)}>slower</button>
68+
<button onclick={() => (milliseconds /= 2)}>faster</button>
8169
```
8270

8371
Teardown functions also run when the effect is destroyed, which happens when its parent is destroyed (for example, a component is unmounted) or the parent effect re-runs.

0 commit comments

Comments
 (0)