Understanding Svelte Runes
Notes on svelte's reactivity system.
Svelte is a tool for building web applications. It allows you to build your app declaratively out of components that combine markup, styles and behaviours. In this blog, we will look at the heart of Svelte ~ reactivity.
In Svelte…
- State refers to the current values held by variables, when the state updates through a reassignment or mutation, it triggers reactivity.
- Reactivity is a mechanism that ensures when your state changes, the DOM and dependent computations automatically update.
- Runes are explicit syntax through which you can mark a variable as reactive, so that when the variable’s value change, reactivity gets triggered.
Runes
There are 7 types of Runes in Svelte :
- $state rune allows you to create reactive state, which means that your UI reacts when it changes. If $state is used with an array or a simple object, we call this deep reactivity.
- $derived rune allows you to derive state from other state. The expression inside the $derived declaration will be re-evaluated whenever its dependencies are updated. $derived.by can be used for complex derivations.
Sample Program :
src/routes/+page.svelte
<script>
const goal = 8;
let glasses = $state(0);
let liters = $derived(glasses * 0.25);
let progress = $derived.by(() => {
Math.min(100, (glasses / goal) * 100);
});
</script>
<p>💧 Water Tracker :</p>
<p>🥤 Glasses: {glasses}</p>
<p>🪣 Liters: {liters}</p>
<p>📊 Progerss: {progress}%</p>
<button
disabled={liters >= goal * 0.25}
onclick={() => {
glasses++;
}}
>
Add a Glass
</button> Output :
💧 Water Tracker :
🥤 Glasses: 0
🪣 Liters: 0
📊 Progerss: 0%
- $effect rune are functions that run when state updates, and can be used for things like calling third-party libraries, drawing on <canvas> elements, or making network requests. You should normally avoid these.
- $props rune are inputs to a component. These can have fallback values, which are used, if the parent component does not set a prop, or its value is undefined.
Sample Program :
$lib/components/Footer.svelte
<script>
let { company = 'Facebook' } = $props();
</script>
<p>© {new Date().getFullYear()} {company}</p> src/routes/+layout.svelte
<script>
import Footer from "$lib/components/Footer.svelte";
</script>
<Footer company="Meta" /> Output :
© 2026 Meta
- $bindable : In Svelte, component props can be bound, which means that data can also flow up from child to parent. This isn’t something you should do often.
- $inspect rune only works during development. It is roughly equivalent to console.log, with the exception that it will re-run whenever its argument changes.
Sample Program :
src/routes/+page.svelte
<script>
let count = $state(0);
$inspect(count);
</script>
<button onclick={() => count++}>
Clicked {count}
{count === 1 ? "time" : "times"}
</button> Output :
Open browser console to see output.
- $host rune provides access to the host element, when compiling a component as a custom elements (aka web components).
Universal Reactivity
Runes can also work outside components. For example, to share some global state.
However, they are not supported in normal .js files, only .svelte.js files.
Sample Program :
$lib/shared.svelte.js
export const counter = $state({
count: 0,
}); $lib/components/Increment.svelte
<script>
import { counter } from "$lib/shared.svelte.js";
</script>
<button onclick={() => (counter.count += 1)}> Add </button> $lib/components/Decrement.svelte
<script>
import { counter } from "$lib/shared.svelte.js";
</script>
<button onclick={() => (counter.count -= 1)}> Subtract </button> src/routes/+page.svelte
<script>
import { counter } from "$lib/shared.svelte";
import Increment from "$lib/components/Increment.svelte";
import Decrement from "$lib/components/Decrement.svelte";
</script>
<p>
Clicked {counter.count}
{counter.count === 1 ? "time" : "times"}.
</p>
<Increment />
<Decrement /> Output :
Clicked 0 times.
Reactive Built-ins
Svelte provides reactive versions of various built-ins like Map, Set and URL that can be used just like their native counterparts.
<script>
import {
MediaQuery,
SvelteDate,
SvelteMap,
SvelteSet,
SvelteURL,
SvelteURLSearchParams,
createSubscriber
} from 'svelte/reactivity';
</script> That’s all Folks
I think Svelte is moving in the right direction with this new reactivity system. By making it explicit, it becomes more predictable, easier to debug, and simpler to maintain. Runes give clarity and control without adding unnecessary overhead, and that balance really hits the sweet spot.
> cd ..