Performance: Dynamic render patterns
This affects how quickly the page updates and how smoothly user interactions feel when Vue dynamically renders or updates components.
<template>
<div>
<component
v-for="item in items"
:is="item.type"
:key="item.id"
/>
</div>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([...])
// stable keys ensure Vue efficiently patches only changed items
</script><template>
<div>
<component v-for="item in items" :is="item.type" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([...])
// items update frequently causing all components to re-render
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Dynamic components without stable key | Many nodes updated | N reflows per update | High paint cost | [X] Bad |
| Dynamic components with stable key | Only changed nodes updated | Minimal reflows | Lower paint cost | [OK] Good |
:is directive do in Vue's dynamic render patterns?:is:is directive in Vue allows you to dynamically choose which component or HTML element to render based on a variable.:is changes the actual rendered tag or component, making the UI flexible.:is = dynamic component switch [OK]:is changes the tag or component shown [OK]:is with event handlers:is binds CSS classes:is creates loopsv-if?v-if is used to conditionally render elements or components based on a boolean expression.<MyComponent v-if='show' /> correctly applies v-if to render MyComponent only if show is true.v-if = conditional render [OK]v-if to show/hide components conditionally [OK]v-if with v-showv-for for conditional rendering:is for boolean conditions<component :is="currentView" />
data() { return { currentView: 'HomeView' } }HomeView is a registered component?<component :is=... /> usage<component> tag with :is dynamically renders the component named by the string in currentView.HomeView is registeredHomeView is registered, Vue will render that component in place of <component>.HomeView component will be rendered. -> Option D:is renders component named by string [OK]component tag as invalid HTML<component :is="viewName" v-if="viewName" />
viewName can be null or a component name string.v-if="viewName" behaviorv-if prevents rendering when viewName is null or falsy, so the component won't render or cause errors.:is binding type:is accepts a string naming a registered component or a component object, so a string is valid.v-if blocks null, :is accepts string [OK]v-if prevents null rendering errors [OK]v-ifv-show with v-if:is only accepts objectsv-for to loop over component namescomponentNames with v-for renders multiple components dynamically.v-if="name"v-if="name" ensures only non-empty strings render components, avoiding errors or empty renders.:key with unique name for list rendering:key="name" helps Vue track each component efficiently.v-if filter + :is = correct pattern [OK]v-for with v-if to filter dynamic components [OK]v-if on parent instead of each item:key in loopsPass the test and this pattern moves unknown