Skip to content

toRef with nested object's property looses reactivity when parent object is reassigned #12338

Answered by Wei-Ba
junhea asked this question in Help/Questions
Discussion options

You must be logged in to vote
const origin = { inner: {} }
const refvalue = ref(origin) // refvalue.value === origin
const refvalue2 = toRef(origin.inner) // refvalue2.value === origin.inner

const other = { inner: {} }
refvalue.value = other // refvalue.value === other   refvalue2.value === origin.inner   origin !== other
refvalue.value.inner.a = 'a' // other.inner.a = 'a'
const a = ref({ label: '' })
const b = toRef(a.value.label) // ref(a.value.label)
b.value = 'b' // a.value.label !== b.value

const c = toRef(a.value, 'label') 
c.value = 'c' // a.value.label === c.value
const a = ref({ inner: { label: ''} })
const inner = a.inner // a.inner is a proxy, like toRef(a.value.inner)
inner.label = 'a' // a update:  a.in…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by junhea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants