Svelte 101 ๐งก Cheatsheet
1. Core Runes (Svelte 5 State)
| Rune | Purpose | Usage |
|---|---|---|
$state | Mutable reactive state | let count = $state(0); |
$derived | Calculated reactive value | let double = $derived(count * 2); |
$effect | Side effects / DOM sync | $effect(() => { console.log(count); }); |
$props | Component inputs | let { name } = $props(); |
$bindable | Two-way binding prop | let { val = $bindable() } = $props(); |
2. Snippets (The New Way to Compose)
Snippets replace legacy slots for complex UI injection.
<!-- Defining a Snippet -->
{#snippet card(title, content)}
<div class="card">
<h2>{title}</h2>
{@render content()}
</div>
{/snippet}
<!-- Rendering a Snippet -->
{@render card('Hello', childrenSnippet)}
3. Modern Control Flow
Svelte 5 uses explicit tags for flow control, improving performance and readability.
- If:
{#if condition} ... {:else} ... {/if} - Each:
{#each list as item, i (item.id)} ... {/each}(always use keys) - Await:
{#await promise} ... {:then data} ... {:catch error} ... {/await} - Key:
{#key value} ... {/key}(re-mount block on change)
4. Props & Events
Events are no longer dispatched via createEventDispatcher. Use callback props instead.
<!-- Child.svelte -->
<script>
let { onAction } = $props();
</script>
<button onclick={onAction}>Click me</button>
<!-- Parent.svelte -->
<Child onAction={() => alert('Clicked!')} />
5. Lifecycle Hooks
| Hook | Svelte 5 Equivalent |
|---|---|
onMount | $effect (runs on mount) |
onDestroy | Return function from $effect |
beforeUpdate | Use $derived or watch state |
afterUpdate | $effect.pre |
6. Custom Elements (Micro-Frontends)
Transform any component into a Web Component.
<svelte:options customElement="my-widget" />
<script>
let { prop = 0 } = $props();
</script>
<button onclick={() => prop++}>{prop}</button>
7. Key Svelte 5 Principles
- No Stores needed: Use exported
$statefor global signals. - Explicit Reactivity: Use
$derivedfor all computed data; never mutate variables inside derived logic. - Effect Cleanup: Always return a cleanup function from
$effectif you create listeners or timers. - Snippets over Slots: Use Snippets for flexible composition.
- Direct Reference: State variables are reactive everywhere they are imported, no need for subscription syntax (
$store).
8. Quick Tips
- Destructuring: Always destructure props from
$props()at the top level. - Binding: Use
bind:valuefor standard input elements. - Performance: Keep
$effectblocks small; only include the specific logic you need to sync with the outside world. - Testing: Since state is exported as variables, you can unit test them without mounting the component.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!