Ref and reactive help you keep track of data that changes in your Vue app. They make your app update automatically when data changes.
Ref and reactive in Composition API in Vue
import { ref, reactive } from 'vue'; const count = ref(0); const user = reactive({ name: 'Alice', age: 25 });
ref is for simple values like numbers, strings, or booleans.
reactive is for objects and arrays to make all their properties reactive.
message.value to get or set it.const message = ref('Hello');
user.name.const user = reactive({ name: 'Bob', age: 30 });
ref by changing its .value property.message.value = 'Hi there!';reactive object directly.user.age = 31;This Vue component uses ref for a simple number and reactive for an object. Buttons update the values, and the UI updates automatically.
<script setup> import { ref, reactive } from 'vue'; const count = ref(0); const user = reactive({ name: 'Emma', age: 28 }); function increment() { count.value++; } function haveBirthday() { user.age++; } </script> <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increase Count</button> <p>User: {{ user.name }}, Age: {{ user.age }}</p> <button @click="haveBirthday">Have Birthday</button> </div> </template>
Always use .value to read or write a ref value inside JavaScript.
For reactive objects, access and update properties directly without .value.
Use ref for simple data and reactive for complex objects or arrays.
ref holds simple reactive values accessed with .value.
reactive makes objects and arrays reactive with direct property access.
Both help Vue update the UI automatically when data changes.
Practice
ref and reactive?Solution
Step 1: Understand
refusagerefholds simple reactive values like numbers or strings and requires accessing the value with.value.Step 2: Understand
reactiveusagereactivewraps objects or arrays to make their properties reactive and accessed directly without.value.Final Answer:
refis for simple values accessed with.value,reactiveis for objects and arrays accessed directly. -> Option CQuick Check:
ref vs reactive difference = D [OK]
- Thinking ref works directly on objects without .value
- Confusing reactive as only for primitive values
- Assuming ref and reactive are interchangeable
Solution
Step 1: Identify correct reactive syntax for objects
reactiveis used to wrap objects to make their properties reactive.Step 2: Check each option
const state = ref({ count: 0 }) usesrefwith an object, which is allowed but requires.valueto access. const state = reactive({ count: 0 }) correctly usesreactivewith an object. const state = reactive(0) usesreactivewith a number, which is invalid. const state = ref(0) usesrefwith a number, which is valid but not for objects.Final Answer:
const state = reactive({ count: 0 }) -> Option BQuick Check:
reactive object syntax = C [OK]
- Using reactive() on primitive values
- Using ref() for objects without .value access
- Confusing reactive and ref usage
import { ref, reactive } from 'vue';
const count = ref(0);
const state = reactive({ clicks: 0 });
count.value++;
state.clicks++;
console.log(count.value, state.clicks);What will be logged to the console?
Solution
Step 1: Understand ref and reactive increments
countis a ref holding 0, incremented bycount.value++to 1.stateis reactive object withclicks0, incremented bystate.clicks++to 1.Step 2: Check console.log output
Loggingcount.valueandstate.clickswill print1 1.Final Answer:
1 1 -> Option DQuick Check:
Increment ref and reactive values = A [OK]
- Forgetting to use .value with ref
- Assuming reactive needs .value
- Mixing up initial values
import { ref } from 'vue';
const user = ref({ name: 'Alice' });
user.name = 'Bob';Solution
Step 1: Understand ref with objects
refwraps the object, so to access or update properties inside, you must use.value.Step 2: Identify the error in property update
The code tries to updateuser.namedirectly, which is incorrect. It should beuser.value.name.Final Answer:
You must use user.value.name to update the name property. -> Option AQuick Check:
Access ref object properties with .value = A [OK]
- Accessing ref object properties without .value
- Confusing ref and reactive usage
- Thinking ref cannot hold objects
name (string) and tags (array). You also want to track a simple counter for submissions. Which setup correctly uses ref and reactive together?Solution
Step 1: Identify correct usage for form object
The form has multiple fields, soreactiveis best to wrap the object withnameandtagsproperties.Step 2: Identify correct usage for simple counter
The submission count is a simple number, sorefis appropriate to track it reactively.Final Answer:
const form = reactive({ name: '', tags: [] }); const submitCount = ref(0); -> Option AQuick Check:
Use reactive for objects, ref for simple values = B [OK]
- Using ref for complex objects without .value
- Using reactive on primitive values
- Mixing up which data needs ref or reactive
Ready to prove it?
Pass the test and this pattern moves unknown
