Skip to content

Svelte 5: Document when not to use $effect #10193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dummdidumm opened this issue Jan 15, 2024 · 2 comments · Fixed by #10680
Closed

Svelte 5: Document when not to use $effect #10193

dummdidumm opened this issue Jan 15, 2024 · 2 comments · Fixed by #10680

Comments

@dummdidumm
Copy link
Member

Describe the problem

People tend to overuse $effect. We should make it clear in the documentation when to actually use it, and when to use other things instead.
This doesn't need to be on the preview docs but should certainly be part of the final docs.

Describe the proposed solution

If we have a more general guide for runes, describe how to ensure reactivity in various ways and try to avoid mentioning $effect as much as possible.

In the API section, within $effect, start by saying that you likely don't need $effect:

  • If you want something to update in reaction to something else, use $derived
  • If you want to have something update from above but also modify it from below (i.e. you want some kind of "writable $derived"), use an object with getters and setters instead (no glitches/dangers of infinite loops, works in SSR)
  • other use cases I can't think of right now that are common

Importance

would make my life easier

@dummdidumm dummdidumm added this to the 5.0 milestone Jan 15, 2024
@ptrxyz
Copy link

ptrxyz commented Jan 16, 2024

In that regard, I don't know if it's more a Kit issue than a Svelte issue, but since it came up on Discord:

The docs should also make clear that $effect does NOT straight up replace $: due to it not being executed on the server and for that reason does not run ruing SSR. Consider the following example (the effect here is a basic summation so this is probably not much of a deal, but it gets the point across):

These are not the same:

<script>
let a = 1
let b = 2

$: c = a + b
</script>

{c}  // server responds with c == 3
<script>
let a = $state(1)
let b = $state(2)
let c = $state(0)

$effect(() => {
  c = a + b
})
</script>

{c}  // server responds with c == 0, then updates the DOM on mount (--> flicker, SEO, etc)

@benmccann
Copy link
Member

I sent a PR for this awhile back that's fallen a bit down the queue: #9940

dummdidumm added a commit that referenced this issue Mar 1, 2024
- explain when not to use `$effect`, closes #10193
- explain that only synchronous reads are tracked, closes #10475
- explain nuance around reruns and object reads, closes #10392
dummdidumm added a commit that referenced this issue Mar 4, 2024
- explain when not to use `$effect`, closes #10193
- explain that only synchronous reads are tracked, closes #10475
- explain nuance around reruns and object reads, closes #10392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants