We use v-for to repeat elements for each item in a list. v-if decides if an element should show or not. Knowing which runs first helps avoid mistakes.
v-for with v-if precedence in Vue
Introduction
Syntax
Vue
<element v-for="item in items" v-if="condition">...</element>
v-for runs before v-if. It creates the list first, then v-if decides which items to show.
For better performance, avoid putting v-if on the same element as v-for. Instead, filter the list in JavaScript or use a computed property.
Examples
Vue
<li v-for="task in tasks" v-if="!task.done">{{ task.name }}</li>
<template> to separate v-for and v-if for clarity.Vue
<template v-for="task in tasks" v-if="!task.done"> <li>{{ task.name }}</li> </template>
Vue
<li v-for="task in filteredTasks">{{ task.name }}</li>Sample Program
This component loops over items. It shows only items where show is true. So Banana is skipped.
Vue
<template>
<ul>
<li v-for="item in items" v-if="item.show" :key="item.id">
{{ item.text }}
</li>
</ul>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([
{ id: 1, text: 'Apple', show: true },
{ id: 2, text: 'Banana', show: false },
{ id: 3, text: 'Cherry', show: true }
])
</script>Important Notes
Remember, v-for runs first, then v-if. So the list is made, then filtered.
Using v-if inside v-for can hurt performance if the list is big.
Better to filter data before looping when possible.
Summary
v-for creates the list first.
v-if decides which items to show after.
For better speed, filter data before using v-for.
Practice
1. In Vue, when using
v-for and v-if on the same element, which happens first?easy
Solution
Step 1: Understand Vue's rendering order
Vue processesv-forfirst to create the list of items.Step 2: Apply
After the list is created,v-iffilteringv-ifdecides which items to actually render.Final Answer:
v-forruns first, thenv-iffilters the rendered items. -> Option CQuick Check:
v-for before v-if = B [OK]
Hint: Remember: loop first, then filter with v-if [OK]
Common Mistakes:
- Thinking v-if filters before looping
- Assuming simultaneous execution
- Believing Vue throws error with both directives
2. Which of the following is the correct syntax to use
v-for with v-if on the same element in Vue?easy
Solution
Step 1: Check directive order on the same element
Vue allowsv-forandv-ifon the same element;v-forshould come first logically but order in code doesn't affect behavior.Step 2: Validate syntax correctness
<li v-for="item in items" v-if="item.active">{{ item.name }}</li>correctly places both directives on the<li>element with proper syntax.Final Answer:
<li v-for="item in items" v-if="item.active">{{ item.name }}</li> -> Option BQuick Check:
Correct syntax with both directives on same element = C [OK]
Hint: Put v-for and v-if on same tag; v-for first is common [OK]
Common Mistakes:
- Swapping v-for and v-if causing confusion
- Using v-if inside v-for incorrectly
- Placing v-for inside v-if template incorrectly
3. Given the Vue template:
and data:
What will be rendered inside the <ul>?
<ul>
<li v-for="item in items" v-if="item.show">{{ item.name }}</li>
</ul>and data:
items = [
{ name: 'A', show: true },
{ name: 'B', show: false },
{ name: 'C', show: true }
]What will be rendered inside the <ul>?
medium
Solution
Step 1: Loop through all items with v-for
Vue creates list items for all three objects: A, B, and C.Step 2: Apply v-if to filter items
Only items withshow: trueare rendered, so B is skipped.Final Answer:
<li>A</li><li>C</li> -> Option AQuick Check:
v-if filters out B, so only A and C show [OK]
Hint: v-for creates all, v-if hides unwanted [OK]
Common Mistakes:
- Assuming v-if filters before v-for
- Expecting all items to render
- Confusing property names in data
4. What is the main issue with this Vue template?
Assuming
<div v-if="item.active" v-for="item in items">{{ item.name }}</div>Assuming
items is an array of objects with active boolean property.medium
Solution
Step 1: Understand v-for and v-if on same element
Vue processesv-forfirst, thenv-iffilters items, but this means all items are looped before filtering.Step 2: Identify performance concern
Usingv-ifwithv-foron the same element can cause unnecessary loops and slow rendering.Final Answer:
Using v-if and v-for on the same element can cause performance issues. -> Option DQuick Check:
v-for then v-if may hurt performance [OK]
Hint: Avoid v-if on same element as v-for for better speed [OK]
Common Mistakes:
- Thinking order causes syntax error
- Believing no performance impact
- Assuming v-if runs before v-for
5. You want to display only active users from a large list efficiently. Which Vue pattern is best to combine
v-for and v-if for performance?hard
Solution
Step 1: Understand performance impact of v-if with v-for
Usingv-ifinsidev-forcauses Vue to loop all items before filtering, which is inefficient for large lists.Step 2: Use computed property to pre-filter data
Filtering the list in a computed property returns only active users, sov-forloops only needed items withoutv-if.Final Answer:
Filter the list in a computed property, then use v-for without v-if. -> Option AQuick Check:
Pre-filter data before v-for for best speed [OK]
Hint: Filter data first, then loop with v-for only [OK]
Common Mistakes:
- Using v-if inside v-for causing slow loops
- Trying to filter with v-if outside v-for incorrectly
- Using nested loops unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
