Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Vue ignores an assignment and warns that the target is readonly

Your error

[Vue warn] Set operation on key "foo" failed: target is readonly.1 more line it can show

Fix it fast

Most common cause, 1 of 3

A child component assigns to its own prop

If the parent should see the change, make it a v-model with defineModel (Vue 3.4 and later).

Fix
<!-- Child.vue: parent writes <Child v-model:title="bookTitle" /> -->
<script setup>
const title = defineModel('title')
function rename() {
  title.value = 'New title'
}
</script>

<!-- or keep a local copy -->
<script setup>
import { ref } from 'vue'
const props = defineProps(['initialCounter'])
const counter = ref(props.initialCounter)
</script>
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

[Vue warn] Set operation on key "x" failed: target is readonly means Vue 3 blocked an assignment, so the value never changed. The target is a prop, an object wrapped in readonly() (often from inject) or a computed such as a store getter. Write to the source instead: defineModel, a provided function or the state. Vue 3.5.

  • Applies to Vue 3.5.46, Pinia 4.1, script setup and Options API
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Take the key name from the warning and find where it is assigned. Then look at where that object comes from: defineProps or this from props is cause 1, inject or readonly() is cause 2, a computed or a store getter is cause 3.

Run this first
grep -rnE "(props|this)\.foo *=|\.foo *=" src/ --include=*.vue --include=*.ts --include=*.js

Then find the line you see in this list and open its fix:

  1. 1A child component assigns to its own propThe key is declared in defineProps or the props option, and the line reads props.foo = ... or this.foo = ...
  2. 2The object was wrapped in readonly(), often by provideThe object comes from inject(...) or a composable that returns readonly(state)
  3. 3Code assigns to a computed value or a store getterThe key is a computed() kept inside a reactive object, or a getter of a Pinia store

What does the error look like?

  • [Vue warn] Set operation on key "foo" failed: target is readonly.
  • [Vue warn] Set operation on key "double" failed: target is readonly.

Cause 1 of 3

A child component assigns to its own prop

You seeThe key is declared in defineProps or the props option, and the line reads props.foo = ... or this.foo = ...

Check

Vue's props guide calls the binding one-way: the parent's value flows down and the child must not overwrite it, so assigning props.foo only prints the readonly warning. Nested fields of an object prop can still be changed, which is why only top-level keys trigger it.

Check
grep -rn "defineProps" src/components/

Fix

If the parent should see the change, make it a v-model with defineModel (Vue 3.4 and later). If the prop is only a starting value, copy it into a local ref and change that.

Fix
<!-- Child.vue: parent writes <Child v-model:title="bookTitle" /> -->
<script setup>
const title = defineModel('title')
function rename() {
  title.value = 'New title'
}
</script>

<!-- or keep a local copy -->
<script setup>
import { ref } from 'vue'
const props = defineProps(['initialCounter'])
const counter = ref(props.initialCounter)
</script>

Verify

The warning is gone and the value on screen changes when the code runs.

Verify
npm run dev

Confirmed by vuejs.org

Did this fix it?

Cause 2 of 3

The object was wrapped in readonly(), often by provide

You seeThe object comes from inject(...) or a composable that returns readonly(state)

Check

readonly() hands out a proxy that refuses every write, nested ones included. The provide/inject guide suggests exactly that wrapper when a provider wants its data safe from injectors, so an injecting component that assigns to it gets the warning.

Check
grep -rn "readonly(" src/

Fix

Keep the write inside the provider. Provide a function next to the value and call it from the injecting component.

Fix
// provider
const count = ref(0)
function increment() { count.value++ }
provide('counter', { count: readonly(count), increment })

// injector
const { count, increment } = inject('counter')
increment()

Verify

Calling the function updates the value everywhere it is shown, with no warning.

Verify
npm run dev

Confirmed by vuejs.org

Did this fix it?

Cause 3 of 3

Code assigns to a computed value or a store getter

You seeThe key is a computed() kept inside a reactive object, or a getter of a Pinia store

Check

Computed properties are getter-only unless you give them a setter. When such a computed sits inside a reactive object, writing to that key produces the Set operation warning instead of the plain computed one. Pinia getters are computed values of the store, so they cannot be assigned either.

Check
grep -rnE "getters:|computed\(" src/stores/

Fix

Write to the state the getter reads, or give the computed both get and set when a writable value really makes sense.

Fix
// instead of store.doubleCount = 10
store.count = 5

// writable computed
const fullName = computed({
  get: () => `${first.value} ${last.value}`,
  set: (v) => { [first.value, last.value] = v.split(' ') },
})

Verify

The derived value updates through its source and the console stays quiet.

Verify
npm run dev

Confirmed by vuejs.org

Did this fix it?

How do I stop it coming back?

  • Treat every prop as input only; reach for defineModel whenever a child needs to change a parent value.
  • Expose state from composables and providers as readonly plus named functions that do the writing.
  • Keep store writes in actions or on state fields, never on getters.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.