Problem with understanding of documentation regarding reactive object and watchEffect (featuring playground) #7375
-
I have a simple vue 3 app:
When I execute it and start to click the button, nothing happens. No console log after each button click. When I copy this into the playground, I get the console log after each click: here. You have to switch the console to the iFrame where the code is executed to see the output. The playground console log seems to work because the playground is not using the browser console.log but an own implementation. Question: What should actually happen? From the way I understand the docu, I would expect to get a console log after every click. I am using v3.2.40 locally and in the playground. Both in development mode. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This behavior is because the Reactivity Object's properties are not accessed, so no dependencies are collected. Modifying the value does not trigger the watchEffect. So you can use Or you can use watch(source, () => {
// ...
}, { deep: true })
const state = reactive({ count: 0 })
watch(state, () => {
/* triggers on deep mutation to state */
}) |
Beta Was this translation helpful? Give feedback.
This behavior is because the Reactivity Object's properties are not accessed, so no dependencies are collected. Modifying the value does not trigger the watchEffect.
So you can use
console.log("watchEffect", reactiveObject.test);
to collecte the dependency.Or you can use
watch
https://vuejs.org/api/reactivity-core.html#watch