pageClass | sidebarDepth | title | description |
---|---|---|---|
rule-details |
0 |
svelte/prefer-writable-derived |
Prefer using writable $derived instead of $state and $effect |
Prefer using writable $derived instead of $state and $effect
- ❗ This rule has not been released yet.
- ⚙️ This rule is included in
"plugin:svelte/recommended"
. - 💡 Some problems reported by this rule are manually fixable by editor suggestions.
This rule reports when you use a combination of $state
and $effect
to create a derived value that can be written to. It encourages using the more concise and clearer $derived
syntax instead.
<script>
/* eslint svelte/prefer-writable-derived: "error" */
const { initialValue } = $props();
// ✓ GOOD
let value1 = $derived(initialValue);
// ✗ BAD
let value2 = $state(initialValue);
$effect(() => {
value2 = initialValue;
});
</script>
The rule specifically looks for patterns where:
- You initialize a variable with
$state()
- You then use
$effect()
or$effect.pre()
to assign a new value to that same variable - The effect function contains only a single assignment statement
When this pattern is detected, the rule suggests refactoring to use $derived()
instead, which provides the same functionality in a more concise way.
Nothing.
- This rule has no options.