vuejs / core

🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
https://vuejs.org/
MIT License
46.71k stars 8.19k forks source link

watch callback has something side effect when calling `createApp` in the callback #6728

Open deot opened 1 year ago

deot commented 1 year ago

Vue version

3.2.39

Link to minimal reproduction

SFC Playground

Steps to reproduce

  1. Open the demo@vue3.2.39
  2. After clicking it, log 1,1,2,2 will appear

CleanShot 2022-09-23 at 18 01 40

What is expected?

  1. open the demo@vue3.2.37
  2. After clicking it, log 1,2,1,2 will appear

CleanShot 2022-09-23 at 18 04 21

What is actually happening?

Log: 1, 1, 2, 2

System Info

No response

Any additional comments?

No response

deot commented 1 year ago

A new problem after #6614 fixed

zhangzhonghe commented 1 year ago

Workaround:

watch(
  () => isActive.value,
  (v) => {
    if (v) {
     loadData()
    }
  },
  // use post
  { flush: 'post' }
)
weidehai commented 1 year ago

Workaround:

watch(
  () => isActive.value,
  (v) => {
    if (v) {
     loadData()
    }
  },
  // use post
  { flush: 'post' }
)

this workaround seems no work,vue will flush pre end post job when render a new component in vue 3.2.39

jh-leong commented 10 months ago

As a workaround, use a sync flush watcher:

watch(
  () => isActive.value,
  v => {
    // ...
  },
  { flush: 'sync' }
)