EveryPage

Unofficial example

Ask the Svelte docs

The Ask this site box, reading svelte.dev/docs. Type a question and it answers from those pages, with links.

Loading the box…

An unofficial example. Ask this site is not affiliated with or endorsed by the Svelte project. The Svelte docs belong to their authors and are read here under their MIT licence.

Questions it answered

Real answers from this box.

  • How do I declare reactive state?

    In runes mode, you declare reactive state with the $state rune, for example let count = $state(0);. Svelte 4 treated a top-level let as implicitly reactive, but in Svelte 5 a variable is reactive only when created using $state. Legacy mode differs: variables declared at the top level of a component are automatically considered reactive there.

    Conditions and behaviour of $state:

    • Deep reactivity (default): If $state is used with an array or a simple object, the result is a deeply reactive state proxy, and updates to properties or methods like push trigger granular updates.
    • Class instances and other objects: Proxification recurses only until Svelte finds something other than an array or simple object, such as a class instance or an object created with Object.create.
    • Raw state: $state.raw avoids deep reactivity; it can only be reassigned, not mutated, so you replace the object or array to update it.
    • Destructuring: Destructured references from reactive values are not reactive and are evaluated at the point of destructuring.
    • Missing $state: A variable that is reassigned and read in a reactive context without $state or $state.raw triggers a warning, because changes will not correctly update the UI.

    Answered from $state • Svelte Docs, Svelte 5 migration guide • Svelte Docs — let → $state, Reactive let/var declarations • Svelte Docs, $state • Svelte Docs — Deep state, $state • Svelte Docs — $state.raw, Compiler warnings • Svelte Docs — non_reactive_update

  • What does $derived do?

    $derived declares derived state: a value computed from other state that updates when that state changes. For example, let doubled = $derived(count * 2); stays in step with count, while a plain variable would keep its original value. Svelte recalculates the derived value when it is next read, after any state it depends on has changed.

    The expression inside $derived(...) should be free of side effects, and Svelte disallows state changes such as count++ inside it. To compute a value from state, $derived is preferred over assigning inside $effect.

    There are two forms:

    • $derived(expression): takes a single expression and is the usual choice for short computations.
    • $derived.by(function): takes a function for complex derivations that don't fit in a short expression. $derived(expression) is equivalent to $derived.by(() => expression).

    Derived values are writable. You can assign to them like $state, but they still re-evaluate when their expression changes.

    Anything read synchronously inside the expression or function body is a dependency. When one of those changes, the derived value is marked dirty. If an expression contains an await, state read after the await is also tracked, though this does not extend to functions the expression calls. To exclude a piece of state from being a dependency, use untrack.

    Updates propagate lazily. Dependents are notified immediately, but the derived value is only re-evaluated when read. If the new value is referentially identical to the previous one, downstream updates are skipped.

    Derived objects and arrays are returned as-is and are not made deeply reactive. Unlike $state, which wraps objects and arrays in deeply reactive proxies, mutating a derived object still affects the underlying state it came from. Destructuring a $derived declaration makes each resulting variable reactive.

    Answered from $derived • Svelte Docs, $derived • Svelte Docs — Understanding dependencies, $derived • Svelte Docs — Update propagation, Best practices • Svelte Docs — $derived, $derived • Svelte Docs — $derived.by, $derived • Svelte Docs — Deriveds and reactivity, $derived • Svelte Docs — Destructuring

  • How do I bind an input's value?

    You bind an input's value with the bind:value directive, as in <input bind:value={message} />, where message is a $state variable. The input's value property then stays in sync with that variable.

    For numeric inputs (type="number" or type="range"), the value is coerced to a number. If a number input is empty or invalid, the bound value is undefined.

    If an <input> has a defaultValue and is part of a form, it reverts to that value instead of an empty string when the form is reset. This applies since 5.6.0. On the initial render, the binding's value takes precedence unless it is null or undefined.

    You can also pass a get and set function pair, bind:value={() => value, (v) => value = v.toLowerCase()}, to validate or transform the value. This requires Svelte 5.9.0 or newer.

    Answered from bind: • Svelte Docs — <input bind:value>, bind: • Svelte Docs — Function bindings

Add this to your own docs

Paste your docs address to try it, free and without signing up. Then it is one line of code.

DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow