[{"id":"svelteintermediate-001","courseId":"svelte-intermediate","domain":"reactivity-state","topic":"Snapshotting state with $state.snapshot","prompt":"A component keeps a settings object in $state. It must pass a plain copy of the current values to structuredClone, an API that doesn't expect a proxy. Which expression produces that copy?","hint":"Some runes read the value; one of them produces a static copy of the proxy.","explain":"A reactive proxy intercepts every read and write, so an API expecting ordinary data may reject it. Svelte can produce a static, plain copy of what a proxy holds at one moment; that copy is the thing to pass.","source":"https://svelte.dev/docs/svelte/$state#$state.snapshot","interaction":{"type":"single"},"options":[{"id":"a","text":"$state.snapshot(settings)","why":"It takes a static copy of the deeply reactive value, free of proxy wrapping, for APIs that don't expect a proxy."},{"id":"b","text":"settings","whyNot":"The proxy is exactly the wrapper the API doesn't expect; passing it unchanged keeps that wrapper."},{"id":"c","text":"$state.eager(settings)","whyNot":"$state.eager only lets the UI show a value sooner while an await is pending; it does not produce a copy."},{"id":"d","text":"$derived(settings)","whyNot":"A derived value is left as-is rather than copied, so it still refers to the same proxy."}],"answer":"a","explanation":"$state.snapshot returns a static, non-proxy copy of a deeply reactive $state value, for handing state to APIs such as structuredClone that don't expect a proxy.","concept":"$state.snapshot returns a static, non-proxy copy of a deeply reactive $state value, for handing state to APIs such as structuredClone that don't expect a proxy.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"let settings = $state({\n  theme: 'dark',\n  size: 2\n});","takeaways":["Pass $state.snapshot(value) to APIs like structuredClone that don't expect a proxy."]},{"id":"svelteintermediate-002","courseId":"svelte-intermediate","domain":"reactivity-state","topic":"Passing state across modules","prompt":"A component holds $state arrays, plain objects and class instances. Which TWO of these statements about how Svelte handles them are correct?","hint":"Think about which kinds of values Svelte can wrap, and what happens to values added later.","explain":"Svelte wraps plain objects and arrays in proxies, and keeps wrapping whatever it finds nested inside them. It stops at anything that is neither an array nor a plain object.","source":"https://svelte.dev/docs/svelte/$state#Passing-state-across-modules","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Pushing a plain object onto a proxied array makes that object deeply reactive as well.","why":"Svelte proxifies values pushed into a proxied array, so the new object is reactive too."},{"id":"b","text":"A class instance is not proxied, so you use $state in its class fields instead.","why":"Proxification leaves class instances alone, so reactivity comes from $state inside the class itself."},{"id":"c","text":"Destructuring a property from proxied state keeps a live link, so later changes reach the variable.","whyNot":"Destructured references are evaluated at the point of destructuring and don't follow later changes."},{"id":"d","text":"Setting a property on the proxy also changes the plain object you originally passed in.","whyNot":"Updating a proxy's properties does not mutate the original object."},{"id":"e","text":"Svelte proxies class instances nested inside state, so their fields are tracked.","whyNot":"Proxification stops at anything that isn't an array or plain object, so class instances are left as they are."},{"id":"f","text":"Passing a class method to onclick keeps this pointing at the instance.","whyNot":"Passed as onclick={todo.reset}, the button becomes this inside reset rather than the Todo."}],"answer":"ab","explanation":"Arrays and plain objects in $state become deep proxies, including objects added later; class instances are not proxied, and destructured values are read once at that moment.","concept":"Arrays and plain objects in $state become deep proxies, including objects added later; class instances are not proxied, and destructured values are read once at that moment.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Put $state in class fields; proxies stop at class instances, and destructured values are copies."]},{"id":"svelteintermediate-003","courseId":"svelte-intermediate","domain":"reactivity-state","topic":"Complex derivations with $derived.by","prompt":"A derived must sum a $state array, and computing the sum takes a loop with several statements. Which declaration computes the sum correctly?","hint":"Plain $derived takes an expression; think about which rune accepts a function.","explain":"A derived recalculates from whatever it reads synchronously, and its body should be free of side effects. A short expression fits inline; a multi-statement computation needs a function body.","source":"https://svelte.dev/docs/svelte/$derived#$derived.by","interaction":{"type":"single"},"options":[{"id":"a","text":"let total = $derived.by(() => { let t = 0; for (const n of numbers) t += n; return t; });","why":"$derived.by takes a function, so a body with a loop and several statements fits inside it."},{"id":"b","text":"let total = $derived(() => { let t = 0; for (const n of numbers) t += n; return t; });","whyNot":"$derived takes an expression, so passing a function makes total hold the function itself, not the sum."},{"id":"c","text":"let total = $derived.by(numbers.reduce((a, n) => a + n, 0));","whyNot":"$derived.by needs a function it can call; a number already computed gives it nothing to run."},{"id":"d","text":"let total = $derived.by(() => { let t = 0; for (const n of numbers) t += n; });","whyNot":"The function has no return statement, so the derived value is undefined."}],"answer":"a","explanation":"$derived.by accepts a function whose body can hold loops and several statements; $derived(expression) is equivalent to $derived.by(() => expression).","concept":"$derived.by accepts a function whose body can hold loops and several statements; $derived(expression) is equivalent to $derived.by(() => expression).","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Use a function body when a derived needs several statements to compute its value."]},{"id":"svelteintermediate-004","courseId":"svelte-intermediate","domain":"reactivity-state","topic":"Derived values recomputed on read","prompt":"In Svelte 5.25 or later, a value declared as let likes = $derived(post.likes) can be reassigned with likes += 1, which temporarily overrides the derived value.","hint":"Check the version named in the statement and how the variable is declared.","explain":"A derived follows its expression and is recalculated when what that expression reads changes. Whether it can also be written to depends on the Svelte version and on how it was declared.","source":"https://svelte.dev/docs/svelte/$derived#Overriding-derived-values","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"From Svelte 5.25 on, a derived declared with let can be reassigned, and the override is temporary."},{"id":"b","text":"False","whyNot":"Deriveds were read-only before 5.25; from 5.25 on, a let-declared one can be overridden."}],"answer":"a","explanation":"Since Svelte 5.25, a $derived declared without const can be reassigned to override its value temporarily; before 5.25 deriveds were read-only.","concept":"Since Svelte 5.25, a $derived declared without const can be reassigned to override its value temporarily; before 5.25 deriveds were read-only.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Deriveds declared with let can be overridden temporarily, which suits optimistic UI."]},{"id":"svelteintermediate-005","courseId":"svelte-intermediate","domain":"reactivity-state","topic":"Effect dependencies from synchronous reads","prompt":"An effect reads color directly in its body, and reads size inside a setTimeout callback that the body schedules. Which change re-runs the effect?","hint":"Think about when Svelte is still inside the effect's run.","explain":"An effect records the reactive values it reads while running, including reads made by functions it calls. Work done later, after the run returns, falls outside that window.","source":"https://svelte.dev/docs/svelte/$effect#Understanding-dependencies","interaction":{"type":"single"},"options":[{"id":"a","text":"A change to color only","why":"color is read synchronously during the run, so it is tracked; the setTimeout read happens later."},{"id":"b","text":"A change to size only","whyNot":"The size read happens after the run, inside a timer, so it is not tracked."},{"id":"c","text":"A change to color or size","whyNot":"A read made after the run returns registers no dependency, so size changes do nothing."},{"id":"d","text":"Neither, because effects run only once","whyNot":"Effects re-run when a tracked value changes, so changing color re-runs this one."}],"answer":"a","explanation":"An effect tracks values read synchronously during its run, including through function calls; values read after an await or inside a setTimeout are not tracked.","concept":"An effect tracks values read synchronously during its run, including through function calls; values read after an await or inside a setTimeout are not tracked.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"$effect(() => {\n  const context = canvas.getContext('2d');\n  context.fillStyle = color;\n  setTimeout(() => {\n    context.fillRect(0, 0, size, size);\n  }, 0);\n});","takeaways":["Reads after an await or inside a setTimeout do not make an effect re-run."]}]