Subscribe on changes!

the ref in v-bind can not trigger compouted

avatar
Aug 15th 2023
const vBind = {
  aa,
}

vBind isnt a reactive param

avatar
Aug 15th 2023

The ref is set on a plain object. so it's not being unwrapped. You are not passing the ref's value to the child, you pass the ref itself to the child. so props.aa is not the changed value of the ref, it is the ref object, and that does not change (oinly its .value does, which you don't access in the computed)

if you actually use props.aa.value, it works:

https://play.vuejs.org/#eNqlUk1v00AQ/SvLXuqoiQ3qLTgRCeqBHtqKInHxZWuP023t3dV+mFSWr9y4IQ4gfgaIA+LXRAj+BbPrOOmh5dIcIs+8N+P3xq+lC6XixgGd0tTkmitLDFin5pngtZLakpbkslbOQjEmGsqxE/hPOlJqWZMDHD3YUV8icduPE1/4zQhnIpfCWMIYmfkd0dORb5ZO5JZLQfKK5zdLZ60Ui2jUZoIgNW5Y5eDw8Hkmuv2KZslFgVu2pPEWTJNePMrGwkKtKmYBqzRoaiaXODbLaBjPaIIIIemTyWRgTBlDmLEBm0z6VQVv5ullkEZeBJ1Iu6s3o2Qe6kWa9Lx5mvgpL2onhI6pNeig5Kv42kiB5w4OcBe+nVegz5Q/hcnotPfmMVZV8t1J6FntAM32/fwK8pt7+tdm7XsZPddgQDeQ0R1mmV6B7eHji1NY4/MOrGXhKmT/B3wNRlbOa+xpSycKlH2HF9S+CkngYvXGHK8tCDOY8kI9swt8/BIO/OEfsr6XexQfhTn80njFIVWPy+uQJqWlMpimAkou4NxXUcsY6rC3CqYEPQak6LqQ2H5q2P+W26swgxuGXhSNyGzufXiurCCu5Cp6hr9x/7Z4SDYu9FY1qteCnFycncbGarwcL2+jQB2RJCGbHx82Pz///vj975f3m19f/3z7hIcIYh7OvI9fu7/DXmfX3ZfN7h+nqGc1

now, if you wrap your plain object in reactive(), the nested ref will be unwrapped, so now v-bind passed the value to the child Comp.

but why this is work? without reactive wrapper

This works because now your aa ref's value returns a reactive object, and JSON.stringify iterates over this object in the child, thereby accessing props.aa.1 during that iteration, registering it as a dep.

avatar
Aug 15th 2023

The ref is set on a plain object. so it's not being unwrapped. You are not passing the ref's value to the child, you pass the ref itself to the child. so props.aa is not the changed value of the ref, it is the ref object, and that does not change (oinly its .value does, which you don't access in the computed)

if you actually use props.aa.value, it works:

https://play.vuejs.org/#eNqlUk1v00AQ/SvLXuqoiQ3qLTgRCeqBHtqKInHxZWuP023t3dV+mFSWr9y4IQ4gfgaIA+LXRAj+BbPrOOmh5dIcIs+8N+P3xq+lC6XixgGd0tTkmitLDFin5pngtZLakpbkslbOQjEmGsqxE/hPOlJqWZMDHD3YUV8icduPE1/4zQhnIpfCWMIYmfkd0dORb5ZO5JZLQfKK5zdLZ60Ui2jUZoIgNW5Y5eDw8Hkmuv2KZslFgVu2pPEWTJNePMrGwkKtKmYBqzRoaiaXODbLaBjPaIIIIemTyWRgTBlDmLEBm0z6VQVv5ullkEZeBJ1Iu6s3o2Qe6kWa9Lx5mvgpL2onhI6pNeig5Kv42kiB5w4OcBe+nVegz5Q/hcnotPfmMVZV8t1J6FntAM32/fwK8pt7+tdm7XsZPddgQDeQ0R1mmV6B7eHji1NY4/MOrGXhKmT/B3wNRlbOa+xpSycKlH2HF9S+CkngYvXGHK8tCDOY8kI9swt8/BIO/OEfsr6XexQfhTn80njFIVWPy+uQJqWlMpimAkou4NxXUcsY6rC3CqYEPQak6LqQ2H5q2P+W26swgxuGXhSNyGzufXiurCCu5Cp6hr9x/7Z4SDYu9FY1qteCnFycncbGarwcL2+jQB2RJCGbHx82Pz///vj975f3m19f/3z7hIcIYh7OvI9fu7/DXmfX3ZfN7h+nqGc1

now, if you wrap your plain object in reactive(), the nested ref will be unwrapped, so now v-bind passed the value to the child Comp.

but why this is work? without reactive wrapper

This works because now your aa ref's value returns a reactive object, and JSON.stringify iterates over this object in the child, thereby accessing props.aa.1 during that iteration, registering it as a dep.

use props.aa.value can registering it as a dep, but why JSON.stringify(props) not, as you said, JSON.stringify can iterates over this object in the child, so JSON.stringify will iterates over props.aa too, that means props.aa.value will be accessing by JSON.stringify, but why not registering it as a dep?

avatar
Aug 15th 2023

the function accesses the ref's .value getter. JSON.stringify does not, as you can see from the printed JSON object - it only prints the ref's internal ._value property, but skips the .value getter which tracks the reactive read.