Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tangjinzhou opened this issue Feb 8, 2022 · 7 comments · Fixed by #5381
Closed
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working

Comments

@tangjinzhou
Copy link
Contributor

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

@edison1105
Copy link
Member

as workaround:

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

@LinusBorg
Copy link
Member

LinusBorg commented Feb 8, 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

@LinusBorg LinusBorg reopened this Feb 8, 2022
@tangjinzhou
Copy link
Contributor Author

triggerRef for shallowRef

@LinusBorg LinusBorg changed the title triggerRef not update when watch array triggerRef not update for shallowRef when watching multiple values (Array argument) Feb 8, 2022
@lidlanca
Copy link
Contributor

lidlanca commented Feb 8, 2022

workaround

its a bug.

} else if (isArray(source)) {
isMultiSource = true
forceTrigger = source.some(isReactive)
getter = () =>
source.map(s => {
if (isRef(s)) {
return s.value

this might be a possible patch

    forceTrigger = source.some(isReactive)||source.some(isShallow)

@edison1105 edison1105 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working labels Feb 9, 2022
@kuang-lingxi
Copy link
Contributor

@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.

let forceTrigger = false
let isMultiSource = false
if (isRef(source)) {
getter = () => source.value
forceTrigger = isShallow(source)
} else if (isReactive(source)) {
getter = () => source
deep = true
} else if (isArray(source)) {
isMultiSource = true
forceTrigger = source.some(isReactive)

if (
deep ||
forceTrigger ||
(isMultiSource
? (newValue as any[]).some((v, i) =>

@edison1105
Copy link
Member

@kuang-lingxi see #2231

@kuang-lingxi
Copy link
Contributor

@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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants