Subscribe on changes!

computed() method did not executed when a array pushed a element

avatar
Aug 9th 2023

Vue version

3.3.3

Link to minimal reproduction

https://play.vuejs.org/#eNqVVF1v2jAU/Su32UOoSmFb3zJg7aZKXdV9aK2mScse0uQCLsaObIdSIf77jh0CgW6V9hAp8T3n3HM/4lV0UZa9RcVREg1sbkTpyLKrylGqxLzUxtGKDI+7lOt5WTkuaE1jo+cUgxSnKlWSHYmChvTaf+VaWUeKH+90oXEIbieOj5vAVBT8EUogQaoOjzNpeYtw4NlN5FeqCPlFkeA5OemS46VLKL7hzCi6uvt8E3ep0IoTcqZiWndfJFxni+w21PhftB8otMEHr7RO1W8YbiyPhXRsuPAle+tNqzqdYxqOaOXVPVJL7kk96bx5ewY2Ub9/eIzMQeOY6FU7Bn6mCLk1zbMZ7zIcEy85xysdeUWD0cFxEOktMllxqtbB6bhSuRMaIkXwCWbw1YL2yspOO8/bsBlmDTpoRKhjD4D6Y+wFWtRKaniuFxzy+oTPc4PVdlJ3tNNxoYGOjoZ1HOkgPOjXm4odxYdjrFPmGF9Eg7E2czq31f1cuF5peMHKDdNoU3UaBRRwQqGBtDid64IlAJsadoD7yjmtRhdFQT4w6G8OQpa+T1O/VrJhSAE9BKDmvZJQ+5uRRpTM+GkT7olim2zrxz2VjHg+5Xx2r5dg7BwGkm9+m2ZL7EWSy8xaQFbNVjdQbGo0Wq3qAz9NWq/RPZB2EnVddJ5Lkc8gcjgrKPzcq97XL8WmEZvyn6kc/uhHewfbEuBtH/me4tupfsSuy5jwC14hGtY9hGP4r/Nu/Qz6rQXAp3VP0r/W9dd7hsJPC861yfw2JiSF4lM3NbqaTN81KxV4UTdyFj/eWEx6D1YrXItBAjOBByHZfC29BoaZ1OI+BrP68Tqc+QslXCeB4+f4l/MHi9EmePlm2LJZYKTbmMvMhF0dvrz9AuutIHahkkC/EPzOuDQq77GGfahUAdstXHD7KVzuQk3u7OXSsbJNUd6oR9aXYhrhmvfT+VfpO7tnvbPAQz+j9R+qEB8t

Steps to reproduce

https://play.vuejs.org/#eNqVVF1v2jAU/Su32UOoSmFb3zJg7aZKXdV9aK2mScse0uQCLsaObIdSIf77jh0CgW6V9hAp8T3n3HM/4lV0UZa9RcVREg1sbkTpyLKrylGqxLzUxtGKDI+7lOt5WTkuaE1jo+cUgxSnKlWSHYmChvTaf+VaWUeKH+90oXEIbieOj5vAVBT8EUogQaoOjzNpeYtw4NlN5FeqCPlFkeA5OemS46VLKL7hzCi6uvt8E3ep0IoTcqZiWndfJFxni+w21PhftB8otMEHr7RO1W8YbiyPhXRsuPAle+tNqzqdYxqOaOXVPVJL7kk96bx5ewY2Ub9/eIzMQeOY6FU7Bn6mCLk1zbMZ7zIcEy85xysdeUWD0cFxEOktMllxqtbB6bhSuRMaIkXwCWbw1YL2yspOO8/bsBlmDTpoRKhjD4D6Y+wFWtRKaniuFxzy+oTPc4PVdlJ3tNNxoYGOjoZ1HOkgPOjXm4odxYdjrFPmGF9Eg7E2czq31f1cuF5peMHKDdNoU3UaBRRwQqGBtDid64IlAJsadoD7yjmtRhdFQT4w6G8OQpa+T1O/VrJhSAE9BKDmvZJQ+5uRRpTM+GkT7olim2zrxz2VjHg+5Xx2r5dg7BwGkm9+m2ZL7EWSy8xaQFbNVjdQbGo0Wq3qAz9NWq/RPZB2EnVddJ5Lkc8gcjgrKPzcq97XL8WmEZvyn6kc/uhHewfbEuBtH/me4tupfsSuy5jwC14hGtY9hGP4r/Nu/Qz6rQXAp3VP0r/W9dd7hsJPC861yfw2JiSF4lM3NbqaTN81KxV4UTdyFj/eWEx6D1YrXItBAjOBByHZfC29BoaZ1OI+BrP68Tqc+QslXCeB4+f4l/MHi9EmePlm2LJZYKTbmMvMhF0dvrz9AuutIHahkkC/EPzOuDQq77GGfahUAdstXHD7KVzuQk3u7OXSsbJNUd6oR9aXYhrhmvfT+VfpO7tnvbPAQz+j9R+qEB8t

What is expected?

computed() method run when a element push to array

What is actually happening?

computed() method do not run

System Info

No response

Any additional comments?

No response

avatar
Aug 9th 2023

Not a bug, by design.

filteredTodos is a computed returning todos.value directly. Executing todos.value.push will not change todos.value, but changing the internal data only.

Option 1. Use variable assignment instead of push method

todos.value = [...todos.value, { id: id++, text: newTodo.value, done: false }]

Option 2. Or really filter the data

const filteredTodos = computed(() => {
  return todos.value.filter(i => !!i)
})

For more questions, you can try to ask in Discussions