Frameworks & Libraries
Svelte Basics
Svelte is a radical approach to building UIs — it compiles your components to efficient, vanilla JavaScript at build time. No virtual DOM overhead, less boilerplate, and the smallest bundles of any UI framework.
What is Svelte?
Svelte is a UI framework that compiles your components into small, efficient JavaScript at build time instead of shipping a framework runtime to the browser. Components are .svelte files with plain-looking HTML, CSS and script. In Svelte 5, reactive state is declared with runes like $state and $derived. SvelteKit is its full-stack framework.
<script>
let count = $state(0);
let double = $derived(count * 2);
</script>
<button onclick={() => count++}>
{count} (double: {double})
</button>
<style>
button { font-weight: bold; } /* scoped to this component */
</style>What Makes Svelte Unique
No Virtual DOM: Compiles to direct DOM operations — no diffing overhead
Minimal Bundle: 5 KB runtime vs React 40KB — faster initial load
Simple Reactivity: let x = 0; x++ re-renders. No useState wrapper needed
Scoped CSS: Styles in .svelte files are automatically scoped
Built-in transitions: transition:fade, animate:flip — no library needed
Two-way binding: bind:value={x} for inputs — no onChange boilerplate
SvelteKit (Full-stack)
File-based routing: src/routes/about/+page.svelte → /about
load() functions: +page.js exports async load() for data fetching
Form Actions: Server-side form handling without JS on client
SSR by default: Pages render on server, then hydrate on client
Adapters: Deploy to Vercel, Cloudflare, Node, static — one config
API routes: +server.js files for REST endpoints
Common Pitfalls
Expecting array/object mutations to trigger reactivity: arr.push(x) — Svelte only detects assignment
Reassign to trigger: arr = [...arr, x]; obj.count++; obj = obj; — or use $: reactive blocks
Forgetting $ prefix when using stores in templates — you get the store object, not the value
Always use $storeName in template. The $ prefix auto-subscribes and auto-unsubscribes on destroy.
Not using SvelteKit for production apps — running Svelte alone without SSR misses SEO and performance benefits
Use SvelteKit for any real app. It adds SSR, routing, form handling, and adapters for deployment.
Over-reactive $: blocks — nesting reactive declarations causes cascade re-runs
Keep $: declarations simple. Use functions for complex logic; call them from $: x = myFn(deps).
Svelte Code Examples
Code Editor
<p>A Svelte 5 component (it needs the Svelte compiler, so it can't run here):</p>
<pre><script>
let count = $state(0);
let double = $derived(count * 2);
</script>
<button onclick={() => count++}>
{count} (double: {double})
</button></pre>
<p>The same behaviour in plain JavaScript, so you can see what Svelte saves you:</p>
<button id="btn">0 (double: 0)</button>Preview
Svelte Basics: summary
Key points
- Styles in a component are scoped automatically.
- Small bundle sizes make Svelte popular for widgets and performance-sensitive sites.
- Its smaller ecosystem means fewer ready-made component libraries than React.
Frequently asked questions
›Is Svelte good for beginners?
Yes. Its syntax stays close to plain HTML, CSS and JavaScript, so there's less framework-specific code to learn.