Skip to main content

Svelte Component Generator

Generate Svelte 5 component boilerplate with the new runes API — $state, $props, $derived, and $effect. Define your props, events, and lifecycle hooks, then copy the ready-to-paste .svelte file. Switch to Svelte 4 legacy mode for older projects.

✓ Svelte 5 runes syntax ✓ TypeScript-first props ✓ Svelte 4 legacy mode

Svelte 5 Runes vs Svelte 4: A Quick Comparison

Concept Svelte 4 Svelte 5 (Runes)
Reactive state let count = 0 let count = $state(0)
Props export let title: string let { title } = $props()
Derived values $: doubled = count * 2 let doubled = $derived(count * 2)
Side effects $: { sideEffect() } $effect(() => { sideEffect() })
Events createEventDispatcher Callback props (onEventName)
Lifecycle onMount / onDestroy $effect + cleanup return

Svelte 5 Props with TypeScript

The runes API brings fully typed props without a separate interface declaration step. You define the type inline with destructuring:

{``}

When to Use Svelte 4 Legacy Mode

Choose Svelte 4 mode when working on existing projects that haven't migrated to Svelte 5, or when using libraries that depend on the legacy compiler behavior (like SvelteKit 1.x projects). The createEventDispatcher pattern and export let syntax remain fully supported in Svelte 5 for backwards compatibility, but new projects should prefer runes.

Component File Checklist

  • Use PascalCase for component filenames (MyButton.svelte)
  • Prefer lang="ts" for TypeScript benefits even in Svelte 4
  • Keep <style> scoped (the default) — use :global() sparingly
  • For Svelte 5, avoid mixing $state with non-reactive objects — deep reactivity needs explicit $state()
  • Use $inspect() during development for reactive value debugging

Frequently Asked Questions

What is the difference between Svelte 5 runes and Svelte 4 reactive syntax?▼

Svelte 5 introduced runes — a set of compiler hints prefixed with $ that make reactivity explicit. Instead of `let count = 0` (Svelte 4, where any top-level let was reactive), you now write `let count = $state(0)`. Similarly, $props() replaces `export let`, $derived() replaces `$:` reactive declarations, and $effect() replaces `onMount` + reactive blocks. Runes work inside any .svelte.ts file, not just components.

How do I pass props in Svelte 5?▼

In Svelte 5, props are declared with `$props()`: `let { title, count = 0 }: { title: string; count?: number } = $props()`. You can destructure with defaults inline. This replaces `export let title: string` from Svelte 4. For spread props, use `let { ...rest } = $props()`. The TypeScript interface provides type safety without a separate `defineProps` call.

How do events work in Svelte 5 vs Svelte 4?▼

In Svelte 4, components emitted events via `createEventDispatcher`. Svelte 5 replaces this with callback props — you accept an `onEventName` function prop and call it directly, which is more TypeScript-friendly and avoids event bubbling surprises. Example: `let { onSubmit }: { onSubmit: (data: FormData) => void } = $props()`. In templates, use `onclick` (lowercase) instead of `on:click`.

What does $effect() do in Svelte 5?▼

$effect() runs a side effect whenever its reactive dependencies change — similar to React's useEffect but without a dependency array. It tracks dependencies automatically. Return a cleanup function to run before the effect re-runs or the component unmounts. Use $effect.pre() to run before DOM updates. For one-time setup, $effect() with no reactive reads acts like onMount.

When should I use $derived() vs computed values?▼

$derived() creates a reactive value that recomputes whenever its dependencies change. Use it for values computed from state: `let total = $derived(items.reduce((s, i) => s + i.price, 0))`. Unlike $effect(), $derived() is pure (no side effects) and lazy (only recomputes when read). For complex multi-statement derivations, use `$derived.by(() => { ... return value; })`.

What is the Svelte component file structure?▼

A .svelte file has three optional sections: `<script>` (or `<script lang='ts'>`) for logic, the template (HTML with Svelte directives, no wrapper tag needed), and `<style>` for scoped CSS. In Svelte 5, use `<script lang='ts'>` at the top. Svelte automatically scopes styles to the component, so `.button` in a component won't affect other `.button` elements globally.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →