Thuta Learning
Vue.js
ExercisesWeb DevelopmentBeginner

Practice: Reactivity & Template Basics

Vue.jsLesson 2140 min

What you'll walk away with

  • Practice Reactivity & Template Basics hands-on
  • Reinforce the skills you've already learned through practice
  • Get comfortable finding bugs, fixing them, and checking your own work

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.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Practice Reactivity & Template Basics hands-on

Core syntax or pattern

vue
<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

  1. 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.

  2. 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.

  3. test and explain</template>

    Apply Practice: Reactivity & Template Basics in a small example and test normal, edge, and failure cases.

Execution flow
  1. requirement
  2. reason and implement
  3. 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

vue
<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>
You should see
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
vue
<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

What best describes the core idea of Practice: Reactivity & Template Basics?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Using computed() like a plain value instead of calling it with ()
  • Leaving out the :key attribute in v-for and letting it default to the index

Now Try It Yourself

Without looking at the code sample, write a counter component from scratch in 5 minutes, then compare it with this code.

You'll know it worked when: 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.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Practice: Reactivity & Template Basics | Thuta Learning