{"record":{"id":"82b50dd1f0229942","repo":"sveltejs/svelte","slug":"state-unsafe-mutation","errorCode":"state_unsafe_mutation","errorMessage":"state_unsafe_mutation\nUpdating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`\nhttps://svelte.dev/e/state_unsafe_mutation","messagePattern":"state_unsafe_mutation\nUpdating state inside `\\$derived\\(\\.\\.\\.\\)`, `\\$inspect\\(\\.\\.\\.\\)` or a template expression is forbidden\\. If the value should not be reactive, declare it without `\\$state`\nhttps://svelte\\.dev/e/state_unsafe_mutation","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":492,"sourceCode":"\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/state_prototype_fixed`);\n\t}\n}\n\n/**\n * Updating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`\n * @returns {never}\n */\nexport function state_unsafe_mutation() {\n\tif (DEV) {\n\t\tconst error = new Error(`state_unsafe_mutation\\nUpdating state inside \\`$derived(...)\\`, \\`$inspect(...)\\` or a template expression is forbidden. If the value should not be reactive, declare it without \\`$state\\`\\nhttps://svelte.dev/e/state_unsafe_mutation`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/state_unsafe_mutation`);\n\t}\n}\n\n/**\n * A `<svelte:boundary>` `reset` function cannot be called while an error is still being handled\n * @returns {never}\n */\nexport function svelte_boundary_reset_onerror() {\n\tif (DEV) {\n\t\tconst error = new Error(`svelte_boundary_reset_onerror\\nA \\`<svelte:boundary>\\` \\`reset\\` function cannot be called while an error is still being handled\\nhttps://svelte.dev/e/svelte_boundary_reset_onerror`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/svelte_boundary_reset_onerror`);\n\t}\n}","sourceCodeStart":474,"sourceCodeEnd":510,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L474-L510","documentation":"Thrown at reactivity/sources.js:162 in the set() function when writing to a $state source while active_reaction is a DERIVED, BLOCK_EFFECT (template/branch), ASYNC, or EAGER_EFFECT ($inspect) reaction, and the source is not already a tracked dependency of that reaction. Svelte forbids mutating state from inside pure reactive computations because it creates unpredictable, often cyclical, update order.","triggerScenarios":"Assigning to a $state variable inside the body of a $derived, inside a template expression, inside an $inspect callback, or inside a block/branch effect. The guard at sources.js:153-163 checks active_reaction flags and whether the source is in current_sources.","commonSituations":"Setting state inside $derived to cache a value; mutating inside a template expression like `{state.x = computed}`; writing inside $inspect; effects that should be $effect but were written as $derived; legacy store .set inside derived.","solutions":["Move the mutation into a $effect (USER_EFFECT) rather than a $derived — effects are allowed to write state.","If the value is computed, return it from the $derived instead of writing to a separate state.","Wrap the write in untrack(() => {...}) only if you are certain it is safe and intentional, but prefer restructuring."],"exampleFix":"// before\nlet a = $state(1);\nlet b = $state(0);\nconst sum = $derived(() => { b = a + 1; return b; }); // writes inside derived\n\n// after\nlet a = $state(1);\nconst sum = $derived(a + 1); // pure derivation","handlingStrategy":"validation","validationCode":"// Do not write $state inside $derived, $inspect, or template expressions.\n// Move writes into $effect, or return values from $derived.\n// If unavoidable, wrap the write in untrack — but prefer restructuring:\n// import { untrack } from 'svelte';\n// untrack(() => { stateVar = next; });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep $derived bodies pure — compute and return, never assign to state.","Use $effect (not $derived) when you need to write state in response to changes.","Avoid assignments inside template expressions; compute in the script instead.","Treat $inspect as read-only observation."],"tags":["svelte","runes","state","derived","reactivity","mutation"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}