Quiz
Six Svelte questions that come up in code review. Each answer is the why, not just the syntax.
Six Svelte design questions
EXAMPLE
# ============================================================
# Q1) Why does a $: reactive statement not re-run when an object property changes?
# ============================================================
# ANSWER: Svelte tracks assignment, not deep mutation.
# user.name = 'Alice' does NOT trigger because user reference is unchanged.
# Fix:
# user = { ...user, name: 'Alice' }; // reassignment triggers reactivity
# Or use a store + update():
# user.update((u) => ({ ...u, name: 'Alice' }));
# ============================================================
# Q2) bind:value vs on:input — when to use which?
# ============================================================
# ANSWER:
# - bind:value two-way binding; cleanest for forms
# - on:input when you also need to inspect/transform the event,
# or block default behaviour
# Both update the variable; bind is the idiomatic default.
# ============================================================
# Q3) Why is my action's destroy() not firing?
# ============================================================
# ANSWER: the action must return { destroy() { ... } }. Returning a plain
# function does NOT register cleanup. The {#if} block must also unmount
# the node — otherwise the action stays attached.
# ============================================================
# Q4) Stores: writable, readable, derived — when which?
# ============================================================
# ANSWER:
# writable: app-owned mutable state (cart items, user)
# readable: external sources you wrap (time, geolocation)
# derived: computed from other stores; keeps a small dependency graph
# ============================================================
# Q5) Why is my SvelteKit form submitting with a full page reload?
# ============================================================
# ANSWER: you forgot 'use:enhance' from '$app/forms'. With it, the form
# submits via fetch and updates the local state without a navigation.
# ============================================================
# Q6) SSR data fetching — load() in +page.server.ts vs +page.ts?
# ============================================================
# ANSWER:
# +page.server.ts runs ONLY on the server; safe for secrets, DB calls
# +page.ts runs on both server (SSR) AND client (navigation);
# do NOT use for secrets
# Default to .server.ts when you need a database or auth.
# ============================================================
# Bonus — when is Svelte the WRONG choice?
# ============================================================
# ANSWER:
# - You need the biggest ecosystem (React still wins on libraries)
# - Your team is happiest in React/Vue and the project is small
# - You require RSC-style streaming on day one (SvelteKit shipped streaming,
# but RSC-equivalent partial hydration is more battle-tested in Next)
# ============================================================
# Scoring
# ============================================================
# 6 / 6 -> ship to production
# 4 / 6 -> revisit svelte/cheatsheet
# < 4 -> the Svelte tutorial walks through these in 30 minutes
Why it matters
Svelte reactivity tracks ASSIGNMENT, not mutation. Reassign to trigger updates (`user = { ...user, name }`), or use a store + `update()` for the same effect. Once that one fact clicks, most "why is my component not re-rendering?" questions answer themselves.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Discussion
Loading…