Question presented to candidate:
"We get an array of orders from an API and need to show them as a list, with only the open ones in a second list. How do you render lists in Vue with v-for, and what are the rules to get it right?"
What a strong answer should cover:
- Syntax:
v-for="item in items", with the index as(item, index) in items;ofworks as well asin; destructuring is allowed. v-foralso iterates objects ((value, key, index)), integer ranges (n in 10starts at 1) and any iterable such as aMaporSet.- Always give each item a stable, unique
:key(an id, not the index) so Vue can reuse and move the right DOM nodes and component state. <template v-for>repeats a group of elements without a wrapper; in Vue 3 its:keygoes on the<template>.- Do not put
v-ifandv-foron the same element: in Vue 3v-ifruns first and cannot see the loop variable. Filter with a computed property instead. - Array changes through mutation methods (
push,splice,sort,reverse) and replacement (filter,map) are all reactive.
Clarifying questions expected:
- "Do items have a stable unique id I can use as the key?"
- "Roughly how many items? Thousands might need virtual scrolling."
Code / implementation expected: Yes. A keyed list plus a computed filtered list.