Subscribe on changes!

triggerRef doesn't trigger the callback of a simple function watch source

avatar
Aug 28th 2023
image

The first case does not work because the value has not changed (always msg.value). The second case works because a new return value is obtained (oldValue - msg.value, newValue - [msg.value])

avatar
Aug 28th 2023
image The first case does not work because the value has not changed (always msg.value). The second case works because a new return value is obtained (oldValue - `msg.value`, newValue - `[msg.value]`)

I thought I can trigger the effect without changing the value by using the triggerRef. And it does trigger when watch the shallowRef directly or use watchEffect. But I don't understand the difference you talked about the two cases above. The array case, both newValue and oldValue are array.

avatar
Aug 28th 2023

I thought I can trigger the effect without changing the value by using the triggerRef.

The watcher's getter function is being triggered by triggerRef. the callback however, is only being run if the watch's getter function returns a different value.

The array example works because each time the getter runs, it returns a new array - that array may contain the same value as the one before, but its a differerent array. So the callback is being run.

avatar
Aug 28th 2023

I thought I can trigger the effect without changing the value by using the triggerRef.

The watcher's getter function is being triggered by triggerRef. the callback however, is only being run if the watch's getter function returns a different value.

The array example works because each time the getter runs, it returns a new array - that array may contain the same value as the one before, but its a differerent array. So the callback is being run.

Alright, I understand that now, thank you!