Counts from Lists
Part of the Fundamentals section of Coddy's Vue journey. Lesson 29 of 55.
A summary can be derived from record fields. This computed ref counts unread books and changes when a book's read field changes.
const unread = computed(() => books.value.filter(book => !book.read).length);Keep one source of truth in the records. A separately incremented counter can become incorrect when items are added, removed or toggled. Deriving the count handles these paths and gives zero for an empty list. A complementary count can use total length minus the first count.
Challenge
MediumUse a computed value to count unfinished records in the supplied array. Display 1 remaining initially in #result. Render a button per record with id toggle- plus its id. Each button toggles that record's done field and the count must follow.
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 Tasks6Working with Lists
Adding List ItemsRemoving by IdComputed Search ResultsCounts from ListsRecap: List Manager