svelte 5 vs svelte 4
via PatrickJS/awesome-cursorrules
Migrate from Svelte 4 to Svelte 5 with runes, snippets, and standardized event handling.
What is svelte 5 vs svelte 4?
This rule documents the key changes in Svelte 5, including the introduction of runes ($state, $derived, $effect) for explicit reactivity, snippets for reusable markup, and standardized HTML event attributes replacing Svelte directives. Use it when upgrading Svelte 4 projects or learning Svelte 5 syntax.
- Replace reactive declarations ($:) with $state(), $derived(), and $effect() runes for explicit reactivity control
- Convert Svelte 4 event directives (on:click) to standard HTML attributes (onclick)
- Eliminate event modifiers by handling preventDefault and other behaviors inline within event handlers
- Use snippets and render tags to create reusable markup chunks and reduce component duplication
- Understand that runes are compiler syntax and should not be imported like regular utilities
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
I'm using svelte 5 instead of svelte 4 here is an overview of the changes.
.cursorrules for Svelte 5
Overview of Changes
Svelte 5 introduces runes, a set of advanced primitives for controlling reactivity. The runes replace certain non-runes features and provide more explicit control over state and effects.
Snippets, along with render tags, help create reusable chunks of markup inside your components, reducing duplication and enhancing maintainability.
Event Handlers in Svelte 5
In Svelte 5, event handlers are treated as standard HTML properties rather than Svelte-specific directives, simplifying their use and integrating them more closely with the rest of the properties in the component.
Svelte 4 vs. Svelte 5:
Before (Svelte 4):
<script>
let count = 0;
$: double = count * 2;
$: {
if (count > 10) alert('Too high!');
}
</script>
<button on:click={() => count++}> {count} / {double}</button>
After (Svelte 5):
<script>
// Define state with runes
let count = $state(0);
// Option 1: Using $derived for computed values
let double = $derived(count * 2);
// Reactive effects using runes
$effect(() => {
if (count > 10) alert('Too high!');
});
</script>
<!-- Standard HTML event attributes instead of Svelte directives -->
<button onclick={() => count++}>
{count} / {double}
</button>
<!-- Alternatively, you can compute values inline -->
<!-- <button onclick={() => count++}>
{count} / {count * 2}
</button> -->
Key Differences:
-
Reactivity is Explicit:
- Svelte 5 uses
$state()to explicitly mark reactive variables $derived()replaces$:for computed values$effect()replaces$: {}blocks for side effects
- Svelte 5 uses
-
Event Handling is Standardized:
- Svelte 4:
on:click={handler} - Svelte 5:
onclick={handler}
- Svelte 4:
-
Runes are Compiler Syntax:
- Do not import
$state,$derived,$effect,$props,$bindable, or$inspect. - Import only regular Svelte utilities that require imports, such as
tick,untrack,mount, orunmount.
- Do not import
-
No More Event Modifiers:
- Svelte 4:
on:click|preventDefault={handler} - Svelte 5:
onclick={e => { e.preventDefault(); handler(e); }}
- Svelte 4:
This creates clearer, more maintainable components compared to Svelte 4's previous syntax by making reactivity explicit and using standardized web platform features.
Related rules
Senior full-stack TypeScript, React, Node.js guidance with clean architecture, testing, and WHY-oriented reasoning.
Quantitative factor research skills for designing, evaluating, and mining alpha factors in equities markets.
Android development with Jetpack Compose, clean architecture, and Material Design 3.
Angular development with Novo Elements UI library using standalone components.
Expert Angular 18 + TypeScript development with Jest, emphasizing clean code and performance.
Manage Kubernetes clusters, add-ons, stacks, and credentials via the Ankra CLI platform.