| | |

Svelte 101 ๐Ÿงก Cheatsheet

โฑ๏ธ Estimated read: 2 min | |

1. Core Runes (Svelte 5 State)

RunePurposeUsage
$stateMutable reactive statelet count = $state(0);
$derivedCalculated reactive valuelet double = $derived(count * 2);
$effectSide effects / DOM sync$effect(() => { console.log(count); });
$propsComponent inputslet { name } = $props();
$bindableTwo-way binding proplet { 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

HookSvelte 5 Equivalent
onMount$effect (runs on mount)
onDestroyReturn function from $effect
beforeUpdateUse $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

  1. No Stores needed: Use exported $state for global signals.
  2. Explicit Reactivity: Use $derived for all computed data; never mutate variables inside derived logic.
  3. Effect Cleanup: Always return a cleanup function from $effect if you create listeners or timers.
  4. Snippets over Slots: Use Snippets for flexible composition.
  5. 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:value for standard input elements.
  • Performance: Keep $effect blocks 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.

Was this helpful?


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!