Explain it simply
Practice: Reactivity & Template Basics, in plain language
This lesson isn't teaching anything new — it's about getting hands-on practice with the reactivity, template directives, event handling, and forms/v-model concepts you learned in the Basics chapter. Building a new component and writing ref/computed yourself will help the syntax stick. We recommend opening your code editor and writing out each task on your own. Give yourself about 5 minutes to try before peeking at the solution — you'll remember it much better that way.
Problem → Why → Solution
The problem — why this concept matters
Using computed() like a plain value instead of calling it with ()
A common mistake
Leaving out the :key attribute in v-for and letting it default to the index
Core concept — understand how it works
4 hands-on tasks to review basic concepts like ref, computed, v-model, v-for, and event handling.
- requirement
- reason and implement
- test and explain
Practice Reactivity & Template Basics hands-on
Core syntax or pattern
<script setup>
import { ref, computed } from 'vue'
// Task 1: counter
const count = ref(0)
// Task 2: fullName
const firstName = ref('')
const lastName = ref('')
const fullName = computed(() => `${firstName.value} ${lastName.value}`.trim())
// Task 3: fruits list
const fruits = ref(['Apple', 'Banana', 'Mango'])
</script>
<template>
<section>
<p>Count: {{ count }}</p>
<button @click="count++">+</button>
<button @click="count--">-</button>
<button @click="count = 0">Reset</button>
</section>
<section>
<input v-model="firstName" placeholder="First name" />
<input v-model="lastName" placeholder="Last name" />
<p>Full name: {{ fullName }}</p>
</section>
<ul v-if="fruits.length">
<li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li>
</ul>
<p v-else>No items</p>
</template>4 hands-on tasks to review basic concepts like ref, computed, v-model, v-for, and event handling.
Read the code step by step
- requirement
<script setup>This lesson isn't teaching anything new — it's about getting hands-on practice with the reactivity, template directives, event handling, and forms/v-model concepts you learned in the Basics chapter. Building a new component and writing ref/computed yourself will help the syntax stick. We recommend opening your code editor and writing out each task on your own. Give yourself about 5 minutes to try before peeking at the solution — you'll remember it much better that way.
- reason and implement
<button @click="count++">+</button>4 hands-on tasks to review basic concepts like ref, computed, v-model, v-for, and event handling.
- test and explain
</template>Apply Practice: Reactivity & Template Basics in a small example and test normal, edge, and failure cases.
- requirement
- reason and implement
- test and explain
Success
Practice Reactivity & Template Basics hands-on
Failure
Using computed() like a plain value instead of calling it with ()
Hold on, let's think about it this way
This lesson isn't teaching anything new — it's about getting hands-on practice with the reactivity, template directives, event handling, and forms/v-model concepts you learned in the Basics chapter. Building a new component and writing ref/computed yourself will help the syntax stick. We recommend opening your code editor and writing out each task on your own. Give yourself about 5 minutes to try before peeking at the solution — you'll remember it much better that way.
Exercises
Task 1: Build a Counter component with three buttons that increment, decrement, and reset a ref value. Task 2: Link 'firstName' and 'fullName' with computed, then wire up two v-model bindings so fullName updates instantly as you type into the input box. Task 3: Render an array of items (a fruits list) with v-for, and use v-if to show a 'No items' message when the list is empty. Task 4: On a button click, use the @click.stop event modifier to stop the event from propagating to the parent.
Code Example
<script setup>
import { ref, computed } from 'vue'
// Task 1: counter
const count = ref(0)
// Task 2: fullName
const firstName = ref('')
const lastName = ref('')
const fullName = computed(() => `${firstName.value} ${lastName.value}`.trim())
// Task 3: fruits list
const fruits = ref(['Apple', 'Banana', 'Mango'])
</script>
<template>
<section>
<p>Count: {{ count }}</p>
<button @click="count++">+</button>
<button @click="count--">-</button>
<button @click="count = 0">Reset</button>
</section>
<section>
<input v-model="firstName" placeholder="First name" />
<input v-model="lastName" placeholder="Last name" />
<p>Full name: {{ fullName }}</p>
</section>
<ul v-if="fruits.length">
<li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li>
</ul>
<p v-else>No items</p>
</template>The counter buttons update the count instantly, typing in either input updates fullName right away, and the fruits list renders each item on screen individually.Try It in 5 Minutes
Without looking at the code sample, write a counter component from scratch in 5 minutes, then compare it with this code.
A Word of Caution
If you combine v-for and v-if on the same element, Vue 3 gives v-if higher priority, which can lead to broken logic — so it's safer to split them onto separate wrapper elements like in this example.
Real-world usage
How to apply it in a real project
Apply Practice: Reactivity & Template Basics in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Without looking at the code sample, write a counter component from scratch in 5 minutes, then compare it with this code.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<script setup>
import { ref, computed } from 'vue'
// Task 1: counter
const count = ref(0)
// Task 2: fullName
const firstName = ref('')
const lastName = ref('')
const fullName = computed(() => `${firstName.value} ${lastName.value}`.trim())
// Task 3: fruits list
const fruits = ref(['Apple', 'Banana', 'Mango'])
</script>
<template>
<section>
<p>Count: {{ count }}</p>
<button @click="count++">+</button>
<button @click="count--">-</button>
<button @click="count = 0">Reset</button>
</section>
<section>
<input v-model="firstName" placeholder="First name" />
<input v-model="lastName" placeholder="Last name" />
<p>Full name: {{ fullName }}</p>
</section>
<ul v-if="fruits.length">
<li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li>
</ul>
<p v-else>No items</p>
</template>Expected: Practice Reactivity & Template Basics hands-on
Show hint
Apply Practice: Reactivity & Template Basics in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- requirement → reason and implement → test and explain
- 4 hands-on tasks to review basic concepts like ref, computed, v-model, v-for, and event handling.
- Using computed() like a plain value instead of calling it with ()
- Apply Practice: Reactivity & Template Basics in a small example and test normal, edge, and failure cases.
Vue.js Guide — Vue.js