Subscribe on changes!

triggerRef not update for shallowRef when watching multiple values (Array argument)

avatar
Feb 8th 2022

Version

3.2.30

Reproduction link

sfc.vuejs.org/

Steps to reproduce

1、watch shallowRef by arr 2、triggerRef

What is expected?

trigger watch

What is actually happening?

not trigger watch

avatar
Feb 8th 2022

as workaround:

watch(...[arr], ()=> {
  console.log('watch [arr] : arr change, but not trigger watch')
})
avatar
Feb 8th 2022

Edit: I'm rather confused why the first one does work at all. It's a shallow ref.

Edit2: Nevermind I missed the whole point

avatar
Feb 8th 2022

triggerRef for shallowRef

avatar
Feb 8th 2022

workaround

its a bug.

https://github.com/vuejs/core/blob/9c304bfe7942a20264235865b4bb5f6e53fdee0d/packages/runtime-core/src/apiWatch.ts#L213-L219

this might be a possible patch

    forceTrigger = source.some(isReactive)||source.some(isShallow)
avatar
Feb 25th 2022

@LinusBorg hi! can i ask a question about watch implementation? why do we treat ref and shallow ref differently, when we use shallow ref, the callback function is forced to fire.I don't seem to see the corresponding instructions on the official documentation.

https://github.com/vuejs/core/blob/5898629d723e82b68e9b17b91bf8b1a8390a3912/packages/runtime-core/src/apiWatch.ts#L204-L215

https://github.com/vuejs/core/blob/5898629d723e82b68e9b17b91bf8b1a8390a3912/packages/runtime-core/src/apiWatch.ts#L307-L311

avatar
Feb 25th 2022

@kuang-lingxi see #2231

avatar
Feb 25th 2022

@kuang-lingxi see #2231

thanks reply!actually I saw this issue yesterday via git commit. In the previous implementation, the check was skipped by judging whether it was a ref. actually I don't really understand why ref skips the check.

or i change the question, what is the expected behavior of the watch function when it accepts ref/shallow ref/reactive object? Is there any documentation on this?