--- title: Svelte date: 2025-08-16 10:30:00 background: bg-[#FF3E00] tags: - svelte - web categories: - Programming intro: | A Svelte cheat sheet with the most important concepts, functions, reactivity, and more. A complete quick reference for beginners, updated for Svelte 5. plugins: - copyCode - runCode --- ## Getting Started ### Basic Syntax ```js
It is {temperature}°C (Warm) in {city}
{:else}It is {temperature}°C in {city}
{/if} ``` Note: Svelte components must always return a root element or content. ## Components ### Functional Component ```js{fullName()}
``` ## Properties {.cols-2} ### Passing Properties to a Component ```jsLoading notifications...
{:else}Count: {$count}
Double: {$double}
``` ### Readable Store ```js import { readable } from 'svelte/store'; export const time = readable(new Date(), function start(set) { const interval = setInterval(() => { set(new Date()); }, 1000); return function stop() { clearInterval(interval); }; }); ``` ### Reactive Declarations ```js{sum}
``` ### Reactive Statements with Side Effects ```js ``` ### Bind to DOM Properties ```js{text.length} characters
``` ### Bind Grouped Inputs (Radio, Checkbox) ```jsSelected: {selected}
``` ### Class and Style Directives ```jsResizable text
``` ### Await Blocks ```js {#await userPromise}Loading...
{:then user}{user.name}
{:catch error}Error: {error.message}
{/await} ``` ### SSR with SvelteKit (Basic Example) ```js // +page.server.js export async function load({ fetch }) { const res = await fetch('/api/data'); const data = await res.json(); return { data }; } ``` ```js // +page.svelte