Skip to content

Commit 8f940ee

Browse files
authored
docs: use function bindings in "when not to use effect" (#15544)
1 parent 190c0c7 commit 8f940ee

File tree

1 file changed

+7
-35
lines changed

1 file changed

+7
-35
lines changed

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

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -280,62 +280,34 @@ You might be tempted to do something convoluted with effects to link one value t
280280
</label>
281281
```
282282

283-
Instead, use callbacks where possible ([demo](/playground/untitled#H4sIAAAAAAAACo1SMW6EMBD8imWluFMSIEUaDiKlvy5lSOHjlhOSMRZeTiDkv8deMEEJRcqdmZ1ZjzzxqpZgePo5cRw18JQA_sSVaPz0rnVk7iDRYxdhYA8vW4Wg0NnwzJRdrfGtUAVKQIYtCsly9pIkp4AZ7cQOezAoEA7JcWUkVBuCdol0dNWrEutWsV5fHfnhPQ5wZJMnCwyejxCh6G6A0V3IHk4zu_jOxzzPBxBld83PTr7xXrb3rUNw8PbiYJ3FP22oTIoLSComq5XuXTeu8LzgnVA3KDgj13wiQ8taRaJ82rzXskYM-URRlsXktejjgNLoo9e4fyf70_8EnwncySX1GuunX6kGRwnzR_BgaPNaGy3FmLJKwrCUeBM6ZUn0Cs2mOlp3vwthQJ5i14P9st9vZqQlsQIAAA==)):
283+
Instead, use `oninput` callbacks or — better still — [function bindings](bind#Function-bindings) where possible ([demo](/playground/untitled#H4sIAAAAAAAAE51SsW6DMBT8FcvqABINdOhCIFKXTt06lg4GHpElYyz8iECIf69tcIIipo6-u3f3fPZMJWuBpvRzkBXyTpKSy5rLq6YRbbgATdOfmeKkrMgCBt9GPpQ66RsItFjJNBzhVScRJBobmumq5wovhSxQABLskAmSk7ckOXtMKyM22ItGhhAk4Z0R0OwIN-tIQzd-90HVhvy2HsGNiQFCMltBgd7XoecV2xzXNV7XaEcth7ZfRv7kujnsTX2Qd7USb5rFjwZkJlgJwpWRcakG04cpOS9oz-QVCuoeInXW-RyEJL-sG0b7Wy6kZWM-u7CFxM5tdrIl9qg72vB74H-y7T2iXROHyVb0CLanp1yNk4D1A1jQ91hzrQSbUtIIGLcir0ylJDm9Q7urz42bX4UwIk2xH2D5Xf4A7SeMcMQCAAA=)):
284284

285285
```svelte
286286
<script>
287287
let total = 100;
288288
let spent = $state(0);
289289
let left = $state(total);
290290
291-
function updateSpent(e) {
292-
spent = +e.target.value;
291+
function updateSpent(value) {
292+
spent = value;
293293
left = total - spent;
294294
}
295295
296-
function updateLeft(e) {
297-
left = +e.target.value;
296+
function updateLeft(value) {
297+
left = value;
298298
spent = total - left;
299299
}
300300
</script>
301301
302302
<label>
303-
<input type="range" value={spent} oninput={updateSpent} max={total} />
303+
<input type="range" bind:value={() => spent, updateSpent} max={total} />
304304
{spent}/{total} spent
305305
</label>
306306
307307
<label>
308-
<input type="range" value={left} oninput={updateLeft} max={total} />
308+
<input type="range" bind:value={() => left, updateLeft} max={total} />
309309
{left}/{total} left
310310
</label>
311311
```
312312

313-
If you need to use bindings, for whatever reason (for example when you want some kind of "writable `$derived`"), consider using getters and setters to synchronise state ([demo](/playground/untitled#H4sIAAAAAAAACpWRwW6DMBBEf8WyekikFOihFwcq9TvqHkyyQUjGsfCCQMj_XnvBNKpy6Qn2DTOD1wu_tRocF18Lx9kCFwT4iRvVxenT2syNoDGyWjl4xi93g2AwxPDSXfrW4oc0EjUgwzsqzSr2VhTnxJwNHwf24lAhHIpjVDZNwy1KS5wlNoGMSg9wOCYksQccerMlv65p51X0p_Xpdt_4YEy9yTkmV3z4MJT579-bUqsaNB2kbI0dwlnCgirJe2UakJzVrbkKaqkWivasU1O1ULxnOVk3JU-Uxti0p_-vKO4no_enbQ_yXhnZn0aHs4b1jiJMK7q2zmo1C3bTMG3LaZQVrMjeoSPgaUtkDxePMCEX2Ie6b_8D4WyJJEwCAAA=)):
314-
315-
```svelte
316-
<script>
317-
let total = 100;
318-
let spent = $state(0);
319-
320-
let left = {
321-
get value() {
322-
return total - spent;
323-
},
324-
set value(v) {
325-
spent = total - v;
326-
}
327-
};
328-
</script>
329-
330-
<label>
331-
<input type="range" bind:value={spent} max={total} />
332-
{spent}/{total} spent
333-
</label>
334-
335-
<label>
336-
<input type="range" bind:value={left.value} max={total} />
337-
{left.value}/{total} left
338-
</label>
339-
```
340-
341313
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](svelte#untrack).

0 commit comments

Comments
 (0)