After diving deep into Svelte 5 runes I can say confidently that derived can do 99% of the reactive state functionality required.
Effect is hardly ever needed.
Effect is hardly ever needed.
Comments
here are more utilities for Svelte 5
https://runed.dev/docs
it might help you for example, watch, context etc..
watch is actually awesome, even less reason to use effect!
Just curious, posts like this make me feel like I'm doing something wrong lol.
My point was more just mundane state changes that can get more complex don't need effects, just more complex derived functions.
Genuinely curious. If it can be done better I'd rather do it that way.
But for the case you exemplified $derived seems to be enough
https://svelte.dev/playground/91e5a47ce72746099c5923e4427dfa36?version=5.25.12
Maybe it's just because its a quick POC. - [svelte] assignment_value_stale is logged on each change. And query on line 12 of createResource is marked with state_referenced_locally warning.
Their purpose is to warn about some behavior that is probably not desired, in this case it is.
The warnings are not relevant in this case
https://runed.dev/docs/utilities/resource
As far as I can tell resource uses watch, which uses effect, under the hood :)
I think my particular app just has a lot of use cases for effect (or an effect wrapper). But I see this take so much it makes me doubt myself haha 😅
The only thing missing to eliminate $effect for good now is a primitive to handle asynchronous state atomically
people certainly seem to abuse effects in React more than anywhere
but it's usually all fixable, which makes me think the issue is more cultural than technical 🤔
That's why useEffect was the tool to solve everything (in a poor way)
you never needed useEffect for that
it has very few "legit" use cases where state is actually "leaving" React's state management 🙂
useSyncExternalStore
useOptimistic
callback refs with cleanup
useDeferedValue
use
All added in the last 2 versions
Before that there was only useEffect
I mean even for simple domain logic, not even for syncing with anything external
entire codebases built like that. "fun" 🥴