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
EasyA 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>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks