Discover how Vue's ref and reactive save you from endless manual updates and bugs!
Why Ref and reactive in Composition API in Vue? - Purpose & Use Cases
Imagine you have a simple webpage where you want to update a number or an object's properties and show those changes instantly on the screen.
Without special tools, you have to write extra code to watch for changes and update the page manually every time something changes.
Manually tracking changes is tiring and easy to forget.
You might miss updating some parts, causing the page to show old or wrong data.
This makes your code messy and hard to fix.
Vue's ref and reactive automatically watch your data.
When data changes, Vue updates the page for you instantly.
This keeps your code clean and your app fast and reliable.
let count = 0; document.getElementById('count').innerText = count; // Need to manually update DOM every time count changes
import { ref } from 'vue'; const count = ref(0); // Vue updates DOM automatically when count.value changes
You can build interactive apps that update instantly without writing extra code to track changes.
Think of a shopping cart where the total price updates immediately when you add or remove items, without refreshing the page.
Ref and reactive help Vue watch your data for changes.
They keep your UI in sync automatically.
This makes your code simpler and your app smoother.
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
