Subscribe on changes!

in watchPostEffect use setTimeout update reactive not effective

avatar
Oct 12th 2022

Vue version

3.2.37

Link to minimal reproduction

https://sfc.vuejs.org/#eNp9UcFugzAM/RUrl7YqBXplUGmadt9hx1xYMC0VJFFiqCbEv8+Btts6abfYee/5+XkUz9bGQ48iEzlhZ9uS8CA1QF41A6i29L6QQplekxRwaDyMI1QllbHuO5imPGEcE/LkB5tLr1xjCTxSb7nTdNY4AqZi3Wh8MVxr1BSBw1JRM2AEl5LU6c14eq1rVAQT1M50sGJ3q6cgqoz2NA+H4s5bj8Etm8kgjaSeNgH5ILVeb6A4wIy8ey9gn4YGW3xvOjT9L9g3cLsN9RTt0zRlcYBgw7QYt+a4voH4Y5mcJ8vm1xTosw2BxHOAi7IyrXEZ+694qWlmzCgRiSWlXVfa+OyN5pvMDHn98FJkN3dScCyhluJEZH2WJL5W4ZJnHxt3TPgVOx7Kq8Xou92HMxePjoWl4JzuGgk3B3Q7h7pCh+4/zQfoH905KF5KTF/eCs+R

Steps to reproduce

in watchPostEffect, new update data.num = 10, the use setTimeout update data.num++

What is expected?

rendering of the page

What is actually happening?

no rendering of the page

System Info

windows

Any additional comments?

i think watchPostEffect listen in data update, is shold be rendering page. new not rendering page , vue2.x watch is ok!

avatar
Oct 12th 2022

This is as expected.

When the setTimeout callback changes data.num, the order of rendering is as follows.

  1. Component render function
  2. WatchPostEffect callback
  3. Component render function

Because the watchPostEffect callback runs with data.num changed to 10, the component always ends up display 10.

avatar
Oct 12th 2022

Your watchPostEffect has data.num as a dependency because of your console.log, so it re-runs after each re-render, re-setting data.num to 10 over and over. This is expected behavior resulting from a badly set up watch effect.

if your remove the console.log, it will show 10, and then 11, and then stop.