Skip to main content

Svelte Reference

v1.0.0

Quick reference for Svelte 5 runes, reactivity, components, and SvelteKit

Svelte 5 quick-reference covering runes ($state, $derived, $effect), reactivity primitives, components, template syntax, stores, SvelteKit routing, actions, and transitions. Search and filter entries by name or category.

How to use
  • Type in the search box to filter entries by name, syntax, or description.
  • Click a category chip to narrow results — click again to clear the filter.
  • Expand any entry to see a syntax snippet and a practical code example.
  • Use the example picker for preset filter scenarios.

50 entries found

$state
Runes

let count = $state(0)

Declare reactive state. The compiler tracks reads and writes and updates the DOM automatically.

$state.raw
Runes

let items = $state.raw([])

Declare state that is never made deeply reactive. Ideal for large read-only arrays or objects.

$derived
Runes

let doubled = $derived(count * 2)

Derived value that recalculates whenever its reactive dependencies change.

$derived.by
Runes

let result = $derived.by(() => expr)

Derived value using a function body — useful when the derivation needs multiple statements.

$effect
Runes

$effect(() => { /* side effect */ })

Run a side effect whenever its reactive dependencies change. Auto-cleans up on destroy.

$effect.pre
Runes

$effect.pre(() => { })

Like $effect but runs before the DOM is updated. Useful for reading layout before a repaint.

$props
Runes

let { name, age = 25 } = $props()

Declare component props with optional defaults. Replaces export let.

$bindable
Runes

let { value = $bindable() } = $props()

Mark a prop as bindable so the parent can use bind:value on it.

$inspect
Runes

$inspect(value)

Debug helper that logs whenever the tracked value changes. Stripped in production builds.

$host
Runes

$host()

Access the host custom element node inside a Svelte component compiled as a custom element.

untrack()
Reactivity

import { untrack } from 'svelte'

Read reactive state without creating a dependency — the surrounding effect will not re-run.

unstate()
Reactivity

import { unstate } from 'svelte'

Get a plain snapshot of a reactive proxy. Useful for serialisation or logging.

tick()
Reactivity

import { tick } from 'svelte'

Returns a promise that resolves after pending state changes have been applied to the DOM.

flushSync()
Reactivity

import { flushSync } from 'svelte'

Synchronously flush all pending reactive updates — forces immediate DOM reconciliation.

createRawSnippet()
Reactivity

import { createRawSnippet } from 'svelte'

Create a snippet from raw HTML — useful for server-side rendering or dynamic content injection.

mount()
Reactivity

import { mount } from 'svelte'

Imperatively mount a Svelte component into a target DOM element.

Component declaration
Components

<script> ... </script> + markup

A .svelte file is a component. Script block for logic, markup below, optional style block.

Snippet
Components

{#snippet name(params)}...{/snippet}

Reusable markup block within a component. Passed as props or used locally.

@render
Components

{@render snippetName(args)}

Render a snippet. Replaces the slot mechanism from Svelte 4.

bind:value
Components

<input bind:value={variable} />

Two-way binding between a form element and a reactive variable.

bind:this
Components

<div bind:this={element}></div>

Get a reference to a DOM element after it is mounted.

on:event (legacy)
Components

onclick={(e) => handler(e)}

In Svelte 5 use native event attributes. The on: directive is Svelte 4 legacy.

{#if}
Template Syntax

{#if cond}...{:else if cond}...{:else}...{/if}

Conditional rendering — only the matching branch is in the DOM.

{#each}
Template Syntax

{#each list as item, i (key)}...{/each}

Iterate over an array. Use a keyed expression for efficient updates.

{#await}
Template Syntax

{#await promise}...{:then val}...{:catch err}...{/await}

Handle promise states declaratively in the template.

{#key}
Template Syntax

{#key expr}...{/key}

Destroy and recreate contents whenever the expression changes.

{@html}
Template Syntax

{@html rawHtml}

Render raw HTML. Warning: XSS risk — only use with trusted content.

{@const}
Template Syntax

{@const variable = expression}

Declare a local constant inside a template block.

{@debug}
Template Syntax

{@debug var1, var2}

Trigger a debugger breakpoint whenever the listed variables change.

class: directive
Template Syntax

class:name={condition}

Conditionally toggle a CSS class on an element.

style: directive
Template Syntax

style:property={value}

Set a single CSS property dynamically.

writable()
Stores

import { writable } from 'svelte/store'

Create a writable store — subscribers are notified on .set() or .update().

readable()
Stores

import { readable } from 'svelte/store'

Create a read-only store whose value is set by an internal start function.

derived()
Stores

import { derived } from 'svelte/store'

Create a store derived from one or more other stores.

get()
Stores

import { get } from 'svelte/store'

Synchronously read a store value without subscribing.

$store syntax
Stores

$storeName

Auto-subscribe to a store in .svelte files using the $ prefix. Unsubscribes on destroy.

+page.svelte
SvelteKit Routing

src/routes/[path]/+page.svelte

The page component rendered for a given route.

+page.ts / +page.server.ts
SvelteKit Routing

export async function load({ params, fetch })

Load function that fetches data for the page. .server.ts runs on the server only.

+layout.svelte
SvelteKit Routing

src/routes/+layout.svelte

Shared layout wrapper — persists across child route navigations.

+server.ts
SvelteKit Routing

export function GET({ params }) { }

API endpoint handler. Export GET, POST, PUT, DELETE, PATCH functions.

+error.svelte
SvelteKit Routing

src/routes/+error.svelte

Custom error page rendered when a load function throws or returns an error.

goto()
SvelteKit Routing

import { goto } from '$app/navigation'

Programmatic navigation to another route.

use:action
Actions & Transitions

<div use:myAction={params}></div>

Attach an action (lifecycle function) to a DOM element for imperative logic.

transition:
Actions & Transitions

transition:fn={params}

Apply an enter + exit transition to an element when it is added/removed from the DOM.

in: / out:
Actions & Transitions

in:fn out:fn

Separate enter and exit transitions for different effects.

animate:flip
Actions & Transitions

animate:flip={{ duration: 300 }}

Animate element position changes inside an each block using the FLIP technique.

onMount()
Utility

import { onMount } from 'svelte'

Run code after the component is first rendered to the DOM.

onDestroy()
Utility

import { onDestroy } from 'svelte'

Run cleanup code when the component is destroyed.

setContext() / getContext()
Utility

import { setContext, getContext } from 'svelte'

Share data down the component tree without props — Svelte equivalent of dependency injection.

createEventDispatcher()
Utility

import { createEventDispatcher } from 'svelte'

Create a dispatcher for custom component events (Svelte 4 pattern, still supported).