Components
Basic Component

Hello {name}!

Component Structure
<script>Component logic (JS/TS)
MarkupHTML template with {expressions}
<style>Scoped CSS (auto-scoped to component)
<script context="module">Runs once per module, not per instance
Reactivity
Reactive Assignments
Reactive Declarations

$: marks reactive declarations and statements

Reactive Rules
Assignment triggerscount += 1 triggers update; obj.x = 1 does too
Array mutationUse arr = [...arr, item] (reassign to trigger)
$: declarationAuto-recomputes when referenced vars change
$: statementRuns side effects reactively
Props
Declaring & Passing Props

{greeting}, {name}!

Spread Props
Events
DOM Events
value = e.target.value} />
Event Modifiers
preventDefaultCalls e.preventDefault()
stopPropagationStops event bubbling
onceHandler fires only once
selfOnly if event.target is the element
captureUse capture phase
Component Events
Bindings
Two-Way Binding
Element & Component Bindings
Binding Types
bind:valueInput/select/textarea value
bind:checkedCheckbox state
bind:groupRadio/checkbox group
bind:thisDOM element reference
bind:clientWidth/HeightRead-only element dimensions
Stores
Writable Store
// store.js import { writable } from "svelte/store"; export const count = writable(0); // Component — auto-subscribe with $
Store Methods
count.set(10); // set value count.update(n => n + 1); // update from current const unsub = count.subscribe(v => console.log(v));
Store Types
writable(val)Read-write store
readable(val, fn)Read-only, set by start function
derived(stores, fn)Computed from other stores
$storeAuto-subscribe syntax in components
Transitions
Built-in Transitions
{#if visible}
Fades in/out
Fly in, fade out
{/if}
Transition Options
fadeOpacity 0 to 1
flyAnimate x/y offset + opacity
slideSlide in/out (height)
scaleScale and fade
drawSVG path stroke animation
durationTransition time in ms
delayDelay before start
Slots
Default & Named Slots
Default header Default content

Title

Body content goes here

Slot Props
{#each items as item} {/each}

{index}: {item.name}

Context
Set & Get Context
Context vs Stores
ContextComponent-tree scoped, not reactive by default
StoresGlobal, reactive, importable anywhere
Context + StorePass a store via context for scoped reactivity
SvelteKit Basics
File-Based Routing
src/routes/ +page.svelte about/+page.svelte blog/[slug]/+page.svelte
Load Functions
// +page.js (runs on client & server) export async function load({ params, fetch }) { const res = await fetch(`/api/posts/${params.slug}`); return { post: await res.json() }; }
Key Files
+page.sveltePage component
+page.js / +page.tsClient/universal load function
+page.server.jsServer-only load / form actions
+layout.svelteShared layout wrapper
+error.svelteError page
+server.jsAPI endpoint (GET, POST, ...)