0
0
Vueframework~20 mins

Dynamic render patterns in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Render Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue 3 component render?

Consider this Vue 3 component using v-if and v-for together:

<template>
  <ul>
    <li v-for="item in items" :key="item.id" v-if="item.visible">{{ item.name }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue'
const items = ref([
  { id: 1, name: 'Apple', visible: true },
  { id: 2, name: 'Banana', visible: false },
  { id: 3, name: 'Cherry', visible: true }
])
</script>

What will be the rendered output inside the <ul>?

Vue
<template>
  <ul>
    <li v-for="item in items" :key="item.id" v-if="item.visible">{{ item.name }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue'
const items = ref([
  { id: 1, name: 'Apple', visible: true },
  { id: 2, name: 'Banana', visible: false },
  { id: 3, name: 'Cherry', visible: true }
])
</script>
A<ul><li>Apple</li><li>Cherry</li></ul>
B<ul></ul>
C<ul><li>Apple</li></ul>
D<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
Attempts:
2 left
💡 Hint

Remember that v-if inside v-for filters items after looping.

state_output
intermediate
2:00remaining
What is the output after clicking the button twice?

This Vue 3 component toggles a message on button click:

<template>
  <button @click="toggle">Toggle</button>
  <p>{{ message }}</p>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(false)
const message = ref('Hidden')
function toggle() {
  show.value = !show.value
  message.value = show.value ? 'Visible' : 'Hidden'
}
</script>

What will the <p> show after clicking the button twice?

Vue
<template>
  <button @click="toggle">Toggle</button>
  <p>{{ message }}</p>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(false)
const message = ref('Hidden')
function toggle() {
  show.value = !show.value
  message.value = show.value ? 'Visible' : 'Hidden'
}
</script>
AError: message is not defined
BVisibleVisible
CHidden
DVisible
Attempts:
2 left
💡 Hint

Each click toggles the state. Think about the state after two toggles.

📝 Syntax
advanced
2:00remaining
Which option correctly uses dynamic component rendering in Vue 3?

You want to render a component dynamically based on a variable currentView. Which option uses the correct syntax?

Vue
<template>
  <component :is="currentView" />
</template>

<script setup>
import { ref } from 'vue'
const currentView = ref('MyComponent')
</script>
A<component :is="'currentView'" />
B<component :is="currentView" />
C<component is="currentView" />
D<component :is="currentView()" />
Attempts:
2 left
💡 Hint

Dynamic component requires binding the is attribute to a variable.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 dynamic list not update on state change?

Given this component:

<template>
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.name }}</li>
  </ul>
  <button @click="addItem">Add Item</button>
</template>

<script setup>
import { reactive } from 'vue'
const items = reactive([
  { id: 1, name: 'One' },
  { id: 2, name: 'Two' }
])
function addItem() {
  items.push({ id: 3, name: 'Three' })
}
</script>

Clicking 'Add Item' does not update the list. What is the cause?

Vue
<template>
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.name }}</li>
  </ul>
  <button @click="addItem">Add Item</button>
</template>

<script setup>
import { reactive } from 'vue'
const items = reactive([
  { id: 1, name: 'One' },
  { id: 2, name: 'Two' }
])
function addItem() {
  items.push({ id: 3, name: 'Three' })
}
</script>
AUsing reactive on an array does not track push mutations properly; use ref([]) instead.
BThe addItem function is not bound to the component instance.
CThe key attribute is missing, so Vue cannot track list items.
DThe items array is not initialized correctly; it should be a plain array.
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes in arrays with reactive vs ref.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using <Suspense> in Vue 3 dynamic rendering?

Vue 3 introduces the <Suspense> component. What is its primary purpose when rendering dynamic components?

AIt disables reactivity temporarily to improve performance.
BIt automatically caches all dynamic components for faster rendering.
CIt forces all child components to render synchronously to avoid flicker.
DIt allows showing fallback content while async components or data are loading.
Attempts:
2 left
💡 Hint

Think about user experience when waiting for slow components.