Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue3: Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'NewTodo'

I got an error message: Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'NewTodo'

That error appear when im trying to reset the input text value inside child component (FormAddTodo.vue).

App.vue:

export default {
  data(){
    return{
      todos: [],
      newTodo: ""
    }
  },
  components: {
    Todos,
    FormAddTodo
  }
}
</script>

<template>
  <div class="container mx-auto">
      <Todos :todos="todos" />
      <div class="py-8"></div>
      <FormAddTodo :NewTodo="newTodo" :Todos="todos" />
  </div>
</template>

FormAddTodo.vue:

<template>
    <div class="formAddTodo">
        <form @submit.prevent="handleAddTodo" class="addTodo">
            <input type="text" class="" placeholder="type new todo here..." v-model="NewTodo">
        </form>
    </div>
</template>

<script>
    export default {
        props: ['NewTodo', 'Todos'],
        methods: {
            handleAddTodo(){
                const colors = ["cyan", "blue", "indigo", "pink"]
                const todo = {
                    id: Math.random(),
                    content: this.NewTodo,
                    color: colors[Math.floor(Math.random() * ((colors.length-1) - 0 + 1) + 0)]
                }

                this.Todos.push(todo)
                this.NewTodo = '' // this line throw the error
            }
        }
    }
</script>
like image 218
Zulfikar Ahmad Avatar asked Nov 21 '25 11:11

Zulfikar Ahmad


2 Answers

You are using v-model="NewTodo" where NewTodo is the props of the component.

Props are not ment to be changed from the child.

Use a diffrent v-model variable and this will work for you.

like image 192
Nitheesh Avatar answered Nov 23 '25 00:11

Nitheesh


Sometimes this problem can occur due to the same names in state, getters, actions. In my case it was this.app (state) and app (getter). As soon as I renamed state property to this._app, the problem is gone.

P.S.: removing getter 'app' was a more elegant solution to the problem (in my case). Getters are like computed, they are only useful if they return a modified version of the state.

like image 27
Dmitry Kurowsky Avatar answered Nov 23 '25 02:11

Dmitry Kurowsky