Svelte 5 Best Practices
Overview
Svelte 5 introduces Runes โ a universal, fine-grained reactivity system that replaces the Svelte 4 compiler-driven reactivity. This page documents production-ready patterns for Svelte 5 (v5.0+).
Key Findings
1. $state for reactive primitives, objects, arrays โ use $state.raw for large data only reassigned
2. $derived for computed values (expression preferred) โ $derived.by for complex logic
3. $effect for side effects only โ avoid state updates in effects, use event handlers instead
4. $props for component props โ treat as mutable, use $derived for computed prop values
5. Snippets replace slots: {#snippet name()}…{/snippet} rendered with {@render name()}
6. Event handlers: onclick not on:click, <svelte:window onkeydown={…}> for global events
7. {@attach} for external lib integration (D3, charts) โ returns cleanup function
8. {@const} for template-scoped constants in #each blocks
9. TypeScript: interface Props, $props(), Snippet<[Type]> for snippet types
Related Topics
- Hugo Documentation Automation
- LLM-Powered Knowledge Bases
- Self-Discovering Documentation
- AI Content Evolution
- Svelte 5 Best Practices
- Svelte 5 Migration Guide
Evolution Notes
Content last updated: 2026-06-07 Next review: 2026-06-14