Menu
CoddyTech

Reactive Refs

Part of the Fundamentals section of Coddy's Vue journey. Lesson 6 of 55.

A ref holds a value Vue can track. This counter starts at 4; clicking the button changes it to 5 and updates the paragraph.

import { ref } from 'vue';
const count = ref(4);
// Template: <p>{{ count }}</p>
// Template: <button @click="count++">Add</button>

The @click directive runs its expression when the button is clicked. In templates Vue unwraps top-level refs, so you write count instead of count.value. A plain local number does not notify the view when it changes.

challenge icon

Challenge

Easy

A reservation starts with 2 seats. Store it in a ref, display it in p#result, and make button#add add one seat on every click.

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals