Skip to content

CORRECT003 · Effect used as onMount

Severity: warning · Category: correctness

Flags an $effect / $effect.pre whose non-empty body reads no reactive value — no $state, $derived, or $props, no store subscription, and no bare function call (foo()). Such an effect runs once after mount and never re-runs. Checked by static (CLI) analysis of component instance scripts.

An $effect that never reacts to anything is an onMount in disguise. Using $effect obscures that intent and misuses the reactivity system; onMount says “run this once when the component mounts” directly.

<script>
import { onMount } from 'svelte';
// Instead of: $effect(() => { element.focus(); });
onMount(() => {
element.focus();
});
</script>