Performance: Generic components
Generic components affect rendering speed and bundle size by reusing code and reducing code duplication.
<template>
<div>
<Card v-for="item in items" :key="item.id" :title="item.title" :content="item.content" />
</div>
</template>
<script setup>
import Card from './Card.vue'
const items = [
{ id: 1, title: 'Title A', content: 'Content A' },
{ id: 2, title: 'Title B', content: 'Content B' },
{ id: 3, title: 'Title C', content: 'Content C' }
]
</script>
<!-- Card.vue -->
<template>
<div class="card">
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</template>
<script setup>
const props = defineProps(['title', 'content'])
</script><template>
<div>
<div class="card">
<h2>{{ title1 }}</h2>
<p>{{ content1 }}</p>
</div>
<div class="card">
<h2>{{ title2 }}</h2>
<p>{{ content2 }}</p>
</div>
<div class="card">
<h2>{{ title3 }}</h2>
<p>{{ content3 }}</p>
</div>
</div>
</template>
<script setup>
const title1 = 'Title A'
const content1 = 'Content A'
const title2 = 'Title B'
const content2 = 'Content B'
const title3 = 'Title C'
const content3 = 'Content C'
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated HTML blocks | High (due to code duplication) | Multiple reflows per block | High paint cost due to many elements | [X] Bad |
| Generic component with props | Low (single component reused) | Single reflow for list | Lower paint cost due to fewer nodes | [OK] Good |
generic components in Vue?defineProps with a generic type requires specifying the prop shape with a type parameter, like { item: T }.defineProps<{ item: T }>(). const props = defineProps<T>(); misses the prop shape object. const props = defineProps(['item']); uses string array syntax, not generic. const props = defineProps({ item: String }); uses a fixed type, not generic.item is a string "Hello"?const props = defineProps<{ item: T }>();
const item = props.item;
return <div>{typeof item}: {item}</div>;typeof operator returns the type of the value at runtime. Since item is "Hello", a string, typeof item is "string".const props = defineProps<{ item: T }>();
const item = props.item;
console.log(item.value);item.valuevalue is valid only if item is a Vue ref. The generic type T could be any type, so item might not have value.defineProps causes syntax error. is incorrect because defineProps is globally available in Vue SFCs. Generic type T must be declared before use. is incorrect because T is assumed declared in the generic context. Props must be defined as an array, not an object. is wrong because props can be defined as an object type.value assumes item is a ref, but it may not be. -> Option AdefineProps<{ items: T[] }>() correctly types the prop as an array of generic type T.v-for to render each itemdefineProps(['items']) and render items directly without slots. lacks generics and slots. Use defineProps<{ items: any[] }>() and hardcode item rendering inside component. uses any losing type safety and hardcodes rendering. Use defineProps<{ items: T }>() and render items without looping. uses T instead of T[] and misses looping.defineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop. -> Option CdefineProps<{ items: T[] }>() and a default slot with v-for passing each item as slot prop. [OK]Pass the test and this pattern moves unknown