Skip to content

triggerRef does not trigger watch(() => ref.value) #9579

Open
@undergroundwires

Description

@undergroundwires

Vue version

3.3.8

Minimal reproduction

This test fails:

import { nextTick } from 'vue'
test('triggerRef with watch', async () => {
    const a = shallowRef(1)
    let aTriggered = false
    watch(() => a.value, () => {
      aTriggered = true
    })
    triggerRef(a)

    await nextTick()  

    expect(aTriggered).toBe(true)
  })

What is expected?

Watching values of manually triggered references should execute the watch callback.

What is actually happening?

Watching the ref itself works fine. According to post by @LinusBorg in #1209 (comment), this is the expected design of Vue.

However, getting value of the ref does not work. So watch(() => a.value, ...) won't work but, watch(a, ...) work. The behavior should be the same and consistent for shallowRefs where state tracking is disabled

watchEffect works fine for these .value calls. This is another undocumented inconsistency with watch.

Official Vue docs state nothing about caching for watch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    need guidanceThe approach/solution in the PR is unclear and requires guidance from maintainer to proceed further.scope: reactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions