0
0
Vueframework~20 mins

Dynamic components with is attribute in Vue - Practice Problems & Coding Challenges

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

Given the following Vue 3 component setup, what will be rendered inside the <component :is="currentComp" />?

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

<script setup>
import { ref } from 'vue'

const HelloComp = {
  template: '<p>Hello!</p>'
}

const ByeComp = {
  template: '<p>Goodbye!</p>'
}

const currentComp = ref(HelloComp)
</script>
A<p>Goodbye!</p>
B<p>Hello!</p>
CNothing renders because components are not globally registered
DError: Unknown component 'HelloComp'
Attempts:
2 left
💡 Hint

Think about what the is attribute does and how the component name is resolved.

state_output
intermediate
2:00remaining
What is the value of currentComp after clicking the button?

Consider this Vue 3 component:

<template>
  <component :is="currentComp" />
  <button @click="toggleComp">Toggle</button>
</template>

<script setup>
import { ref } from 'vue'

const currentComp = ref('CompA')

function toggleComp() {
  currentComp.value = currentComp.value === 'CompA' ? 'CompB' : 'CompA'
}
</script>

Initially, currentComp is 'CompA'. What is its value after the button is clicked twice?

AError: currentComp is not reactive
BCompB
CCompA
Dundefined
Attempts:
2 left
💡 Hint

Each click toggles the component name. Think about toggling twice.

📝 Syntax
advanced
2:00remaining
Which option correctly uses the is attribute with a dynamic component in Vue 3?

Choose the correct syntax to render a dynamic component using the is attribute bound to a variable currentView.

A<component v-bind:is="currentView" />
B<component is="currentView" />
C<component :is='currentView'/>
D<component :is="currentView" />
Attempts:
2 left
💡 Hint

Remember how to bind attributes dynamically in Vue templates.

🔧 Debug
advanced
2:00remaining
Why does this dynamic component fail to render?

Given this Vue 3 code snippet:

<template>
  <component :is="currentComp" />
</template>

<script setup>
import { ref } from 'vue'

const MyComp = {
  template: '<p>Hi there</p>'
}

const currentComp = ref('MyComp')
</script>

Why does the component not render and show an error?

AMyComp is not registered globally or locally, so Vue cannot find it
BThe template string in MyComp is invalid HTML
CThe ref variable currentComp should not be reactive for dynamic components
DThe component tag must be self-closing with a slash
Attempts:
2 left
💡 Hint

Think about how Vue finds components by name.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using the is attribute for dynamic components in Vue?

Choose the best explanation for why Vue developers use the is attribute to render dynamic components.

AIt allows switching between different components at runtime without changing the template structure
BIt improves performance by caching all components automatically
CIt forces Vue to render components only once and never update them
DIt enables using components without importing or registering them
Attempts:
2 left
💡 Hint

Think about flexibility in UI rendering.