0
0
Vueframework~10 mins

Dynamic render patterns in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dynamic render patterns
Start Component Setup
Define reactive state
Template uses dynamic rendering
User interaction or state change
Update reactive state
Vue reactivity triggers re-render
DOM updates with new content
Wait for next interaction or state change
This flow shows how Vue dynamically renders content by reacting to state changes and updating the DOM accordingly.
Execution Sample
Vue
import { ref } from 'vue'

const currentView = ref('home')

// Template uses <component :is="currentView" />

function switchView(view) {
  currentView.value = view
}
This code switches the displayed component dynamically based on the reactive variable currentView.
Execution Table
StepActionReactive State (currentView)Rendered ComponentDOM Update
1Initialize currentView'home'HomeComponentRender HomeComponent
2User clicks to switch to 'about''home' -> 'about'AboutComponentReplace HomeComponent with AboutComponent
3User clicks to switch to 'contact''about' -> 'contact'ContactComponentReplace AboutComponent with ContactComponent
4User clicks to switch back to 'home''contact' -> 'home'HomeComponentReplace ContactComponent with HomeComponent
5No further interaction'home'HomeComponentNo DOM change
💡 No more user interactions; reactive state remains stable.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
currentView'home''about''contact''home''home'
Key Moments - 2 Insights
Why does changing currentView update the displayed component automatically?
Because currentView is a reactive ref, Vue tracks its changes and triggers re-rendering of the <component :is="..." /> element, as shown in steps 2-4 of the execution_table.
What happens if currentView is set to a component name that does not exist?
Vue will try to render the component but fail silently or show nothing, because the dynamic component name does not match any registered component. This is not shown in the table but is important to handle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of currentView at Step 3?
A'about'
B'contact'
C'home'
D'contactComponent'
💡 Hint
Check the 'Reactive State (currentView)' column at Step 3 in the execution_table.
At which step does the DOM update to show the AboutComponent?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'DOM Update' column in the execution_table for when AboutComponent is rendered.
If the user never clicks to change views, what will the rendered component be?
AAboutComponent
BContactComponent
CHomeComponent
DNo component
💡 Hint
Check the initial value of currentView and the rendered component at Step 1 in the execution_table.
Concept Snapshot
Dynamic render patterns in Vue use reactive state to choose which component to show.
Use <component :is="stateVar" /> in template.
Change the reactive variable to switch views.
Vue updates DOM automatically on state change.
Ensure component names match registered components.
This enables flexible UI changes without full reload.
Full Transcript
This visual execution shows how Vue dynamically renders components based on reactive state changes. Initially, the reactive variable currentView is set to 'home', so the HomeComponent is rendered. When the user clicks to switch views, currentView changes to 'about' or 'contact', triggering Vue's reactivity system to update the DOM and show the corresponding component. The execution table tracks each step, showing the reactive state, rendered component, and DOM updates. Key moments clarify why reactivity triggers rendering and what happens if an unknown component name is used. The quiz tests understanding of state values and DOM changes at each step. This pattern allows building flexible interfaces that respond instantly to user actions.