Svelte · Senior · Question 67 of 75
Why can't a $derived expression change state?
let count = $state(0);
let doubled = $derived(count++ * 2); // ?- ✓Correct answer: Deriveds must be pure; Svelte throws on state writes inside them, because they may run lazily and any number of times
- 2It works and increments
counton each read - 3It is allowed only in
$derived.by
Explanation
Side effects belong in event handlers, or, as a last resort, in effects.