Consider this Vue 3 component using the Composition API. What will be displayed in the browser?
<script setup> import { ref, reactive } from 'vue' const count = ref(0) const state = reactive({ clicks: 0 }) function increment() { count.value++ state.clicks++ } </script> <template> <button @click="increment">Click me</button> <p>Count: {{ count }}</p> <p>Clicks: {{ state.clicks }}</p> </template>
Remember that ref values update with .value and reactive objects update their properties directly.
The count is a ref, so its value is accessed and updated with .value. The state is reactive, so its properties update reactively. Both update correctly on each click.
Which code snippet correctly creates a reactive object with nested properties that Vue tracks reactively?
Think about how reactive and ref handle nested objects.
Using reactive on an object with nested properties makes all nested properties reactive automatically. Wrapping nested objects in ref inside reactive or vice versa is unnecessary and can cause confusion.
Given this Vue component, why does the template not update when state.count changes?
<script setup> import { reactive } from 'vue' const state = reactive({ count: 0 }) function increment() { state.count = state.count + 1 } </script> <template> <button @click="increment">Add</button> <p>{{ state.count }}</p> </template>
Check how reactive objects and their properties are accessed and updated.
reactive creates a reactive proxy for the object. Its properties can be accessed and updated directly without .value. The template automatically tracks these changes and updates.
Given this code snippet, what is the final value of count.value?
<script setup> import { ref } from 'vue' const count = ref(0) function update() { count.value += 1 count.value = count.value * 2 } update() update() </script>
Calculate step by step what happens to count.value after each update() call.
Initially, count.value is 0.
First update(): count.value += 1 → 1, then count.value = 1 * 2 → 2.
Second update(): count.value += 1 → 3, then count.value = 3 * 2 → 6.
Final value is 6.
Choose the correct statement about ref and reactive in Vue's Composition API.
Think about what types each function is designed to handle.
ref creates a reactive container for any value, including primitives and objects. reactive creates a reactive proxy for objects only. They are not interchangeable and have different use cases.