Menu
CoddyTech

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 icon

Challenge

Easy

A 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>
quiz iconTest yourself

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

All lessons in Fundamentals