Reactive Objects
Part of the Fundamentals section of Coddy's Vue journey. Lesson 9 of 55.
Use reactive to track fields that belong together. This transfer changes two fields of the same object; the next render shows 3 ready and 2 pending.
import { reactive } from 'vue';
const jobs = reactive({ ready: 2, pending: 3 });
function transfer() { jobs.ready++; jobs.pending--; }Access reactive object fields directly in both scripts and templates. Unlike a ref holding a primitive, the object itself is a proxy and does not need .value. Keep reads connected to the object: copying a numeric field into an ordinary variable does not create a reactive alias.
Challenge
EasyUse one reactive object for a parcel desk: packed: 1 and waiting: 5. Show Packed: 1, waiting: 5 in #result. Each click of #pack moves one parcel from waiting to packed.
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