Legacy reactive statements are not allowed in components using runes – mixing $: with $state in Svelte 5
0 reputation · 10 Apr 2022, 05:52 UTC
0 reputation · 10 Apr 2022, 05:52 UTC
The goal is to migrate a component to Svelte 5’s runes reactivity gradually, keeping existing legacy reactive statements ($:) for parts of the logic that are not yet converted, while using $state, $derived, or $effect for new features.
However, the Svelte 5 compiler emits the error “Legacy reactive statements are not allowed in components using runes” when both syntaxes appear in the same file, blocking a piecemeal approach. It is unclear whether a compiler flag or future release will relax this restriction, or if the only supported path is to extract legacy logic into a separate child component.
$: statements in runes‑mode components for incremental migration?$: and runes in a future Svelte 5.x release?35775 reputation · 10 Apr 2022, 06:58 UTC
There is no compiler flag that lets you keep legacy $: statements in a component that also uses Svelte 5 runes ($state, $derived, $effect). The compiler treats any file containing a rune as rune‑mode and will emit the error “Legacy reactive statements are not allowed in components using runes”. Consequently, the only supported way to migrate incrementally is to extract the legacy logic into a separate child component that does not use runes.
$:) or rune‑mode (any rune). Mixing both triggers a compile‑time error.$: and runes to coexist in the same file.$: blocks you want to keep unchanged.LegacyLogic.svelte) that contains only those $: statements and does not import or use any runes.bind:this or callback props if the child needs to affect parent state.$: blocks from the parent file.$state, $derived, $effect for new features.If you need to share state without creating a child component, the only alternative is to move the legacy logic into a plain JavaScript/TypeScript module that exports functions or a Svelte store, then import that module in both the parent and any other components. This still avoids mixing syntaxes in a single Svelte file.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.