reactive and toRefs. What will be the rendered output after clicking the button once?<template>
<div>
<p>{{ state.count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({ count: 0 })
function increment() {
state.count++
}
</script>The reactive function creates a deeply reactive object. When state.count changes, Vue tracks it and updates the DOM. Initially, count is 0. After clicking, it increments to 1 and the displayed value updates accordingly.
nested.value after mutation?nested.value after running state.nested.value = 10?<script setup> import { reactive } from 'vue' const state = reactive({ nested: { value: 5 } }) state.nested.value = 10 </script>
Vue's reactive creates deep reactivity. Changing state.nested.value updates the reactive object. So the new value is 10.
ref with a nested object. Why does the template not update when state.value.count changes?<template>
<p>{{ state.value.count }}</p>
<button @click="increment">Increment</button>
</template>
<script setup>
import { ref } from 'vue'
const state = ref({ count: 0 })
function increment() {
state.value.count++
}
</script>ref tracks changes to the reference itself, not deep properties inside the object. Changing state.value.count does not trigger reactivity. To track nested changes, use reactive or replace the whole object.
reactive creates a deeply reactive object. ref tracks only the reference. Wrapping ref inside reactive or vice versa is incorrect and causes errors or unexpected behavior.
Deep reactivity means Vue tracks changes inside nested objects and arrays automatically. This allows the UI to update whenever any nested data changes, making the app reactive and responsive without extra code.