-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Open
Description
Vue version
3.5.20
Link to minimal reproduction
Steps to reproduce
- Use this code
import { ref, toRef, watchEffect, computed } from 'vue' const myObj = ref({ id: 'foo' }) setInterval(() => { myObj.value = { id: 'foo' } }, 2000) const id1 = toRef(() => myObj.value.id) const id2 = computed(() => myObj.value.id) watchEffect(() => console.log('Changed (toRef)', id1.value)) watchEffect(() => console.log('Changed (computed)', id2.value))
- Check the console for logs
What is expected?
Both watchEffect
to be executed once.
Using toRef
with a getter should create a computed
(or at least have the same behavior as computed
)
What is actually happening?
watchEffect
using the value created with toRef
triggers every time the source object is updated, even if the the getter produces the same result.
Using computed
with a getter creates a ComputedRef
, which triggers reactivity only when its result change.
Using toRef
with a getter creates a Readonly<Ref>>
, which triggers reactivity even if its result doesn't change.