Updating Refs in Script
Part of the Fundamentals section of Coddy's Vue journey. Lesson 7 of 55.
JavaScript accesses a ref through its value property. Calling this handler twice takes credits from 10 to 4.
const credits = ref(10);
function spend() {
credits.value -= 3;
}
// Template: <button @click="spend">Spend</button>Template unwrapping does not apply inside JavaScript functions. The const binding keeps the ref object itself stable; updating its value is allowed. Read the current value when the handler runs so repeated clicks accumulate.
Challenge
EasyA supply shelf starts with 9 units. Complete useStock() so each click of #use subtracts 2 and #result stays synchronized.
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