4 chapters · 13 min
Skills
Chapter 4 of 4
Guidance on writing fast, robust, modern Svelte code. Load this skill whenever in a Svelte project and asked to write/edit or analyze a Svelte component or module.
3 minutes · 755 words · 13 sections
$stateOnly use the $state rune for variables that should be reactive — in other words, variables that cause an $effect, $derived or template expression to update. Everything else can be a normal variable.
Objects and arrays ($state({...}) or $state([...])) are made deeply reactive, meaning mutation will trigger updates. This has a trade-off: in exchange for fine-grained reactivity, the objects must be proxied, which has performance overhead. In cases where you’re dealing with large objects that are only ever reassigned (rather than mutated), use $state.raw instead. This is often the case with API responses, for example.
$derivedTo compute something from state, use $derived rather than $effect:
// do this
let square = $derived(num * num);
// don't do this
let square;
$effect(() => {
square = num * num;
});[!NOTE]
$derivedis given an expression, not a function. If you need to use a function (because the expression is complex, for example) use$derived.by.
Deriveds are writable — you can assign to them, just like $state, except that they will re-evaluate when their expression changes.
If the derived expression is an object or array, it will be returned as-is — it is not made deeply reactive. You can, however, use $state inside $derived.by in the rare cases that you need this.
$effectEffects are an escape hatch and should mostly be avoided. In particular, avoid updating state inside effects.
{@attach ...} (opens in a new tab)$inspect (opens in a new tab)createSubscriberNever wrap the contents of an effect in if (browser) {...} or similar — effects do not run on the server.
$propsTreat props as though they will change. For example, values that depend on props should usually use $derived:
// @errors: 2451
let { type } = $props();
// do this
let color = $derived(type === 'danger' ? 'red' : 'green');
// don't do this — `color` will not update if `type` changes
let color = type === 'danger' ? 'red' : 'green';$inspect.trace$inspect.trace is a debugging tool for reactivity. If something is not updating properly or running more than it should you can add $inspect.trace(label) as the first line of an $effect or $derived.by (or any function they call) to trace their dependencies and discover which one triggered an update.
Any element attribute starting with on is treated as an event listener:
<button onclick={() => {...}}>click me</button>
<!-- attribute shorthand also works -->
<button {onclick}>...</button>
<!-- so do spread attributes -->
<button {...props}>...</button>If you need to attach listeners to window or document you can use <svelte:window> and <svelte:document>:
<svelte:window onkeydown={...} />
<svelte:document onvisibilitychange={...} />Avoid using onMount or $effect for this.
Snippets (opens in a new tab) are a way to define reusable chunks of markup that can be instantiated with the {@render ...} (opens in a new tab) tag, or passed to components as props. They must be declared within the template.
{#snippet greeting(name)}
<p>hello {name}!</p>
{/snippet}
{@render greeting('world')}[!NOTE] Snippets declared at the top level of a component (i.e. not inside elements or blocks) can be referenced inside
<script>. A snippet that doesn’t reference component state is also available in a<script module>, in which case it can be exported for use by other components.
Prefer to use keyed each blocks (opens in a new tab) — this improves performance by allowing Svelte to surgically insert or remove items rather than updating the DOM belonging to existing items.
[!NOTE] The key must uniquely identify the object. Do not use the index as a key.
Avoid destructuring if you need to mutate the item (with something like bind:value={item.count}, for example).
If you have a JS variable that you want to use inside CSS you can set a CSS custom property with the style: directive.
<div style:--columns={columns}>...</div>You can then reference var(--columns) inside the component’s <style>.
The CSS in a component’s <style> is scoped to that component. If a parent component needs to control the child’s styles, the preferred way is to use CSS custom properties:
<!-- Parent.svelte -->
<Child --color="red" />
<!-- Child.svelte -->
<h1>Hello</h1>
<style>
h1 {
color: var(--color);
}
</style>If this is impossible (for example, the child component comes from a library) you can use :global to override styles:
<div>
<Child />
</div>
<style>
div :global {
h1 {
color: red;
}
}
</style>Consider using context instead of declaring state in a shared module. This will scope the state to the part of the app that needs it, and eliminate the possibility of it leaking between users when server-side rendering.
Use createContext rather than setContext and getContext, as it provides type safety.
If using version 5.36 or higher, you can use await expressions (opens in a new tab) and hydratable (opens in a new tab) to use promises directly inside components. Note that these require the experimental.async option to be enabled in svelte.config.js as they are not yet considered fully stable.
Always use runes mode for new code, and avoid features that have more modern replacements:
$state instead of implicit reactivity (e.g. let count = 0; count += 1)$derived and $effect instead of $: assignments and statements (but only use effects when there is no better solution)$props instead of export let, $$props and $$restPropsonclick={...} instead of on:click={...}{#snippet ...} and {@render ...} instead of <slot> and $$slots and <svelte:fragment><DynamicComponent> instead of <svelte:component this={DynamicComponent}>import Self from './ThisComponent.svelte' and <Self> instead of <svelte:self>$state fields to share reactivity between components, instead of using stores{@attach ...} instead of use:actionclass attributes, instead of the class: directiveInstall this repository
npx skills add sveltejs/ai-tools/plugin marketplace add sveltejs/ai-toolsSkills install per repository, not per chapter — the CLI has no documented per-skill form, so we do not print one.
Guidance on writing fast, robust, modern Svelte code. Load this skill whenever in a Svelte project and asked to write/edit or analyze a Svelte component or module. Covers reactivity, event handling, styling, integration with libraries and more.
The verbatim description from this skill’s front matter — the string an agent matches on to decide whether to load it.
tools/skills/svelte-core-bestpractices/SKILL.mdmain, last pushed 10 August 2026.SKILL.md, not by matching a directory convention. 2 distinct layouts observed: .agents/skills/*/SKILL.md, tools/skills/*/SKILL.md..claude-plugin/marketplace.json by Svelte, declaring 1 plugin. It is read for editorial metadata only — never as the skill index, which is always the repository tree./sveltejs/ai-tools.md, and each chapter at its own .md URL.9 files · 30 KB
Everything this skill ships beside its prose. All of it is set here, as subchapters of chapter 4.
Documentation the agent loads on demand, rather than up front.