Performance: Compound components pattern
MEDIUM IMPACT
This pattern affects rendering speed and interaction responsiveness by managing how nested components communicate and update.
CompoundComponent.vue <template> <slot name="input" :value="value" :updateValue="updateValue" /> <slot name="display" :value="value" /> </template> <script setup> import { ref } from 'vue' const value = ref('') function updateValue(newVal) { value.value = newVal } </script> Usage.vue <template> <CompoundComponent> <template v-slot:input="{ value, updateValue }"> <input :value="value" @input="e => updateValue(e.target.value)" /> </template> <template v-slot:display="{ value }"> <p>{{ value }}</p> </template> </CompoundComponent> </template>
Parent.vue
<template>
<div>
<ChildA :value="value" @update="value = $event" />
<ChildB :value="value" />
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref('')
</script>
ChildA.vue
<template>
<input :value="value" @input="$emit('update', $event.target.value)" />
</template>
<script setup>
const props = defineProps({ value: String })
</script>
ChildB.vue
<template>
<p>{{ value }}</p>
</template>
<script setup>
const props = defineProps({ value: String })
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Prop drilling with multiple child components | High - many props and emits | Multiple reflows per input | High paint cost due to frequent updates | [X] Bad |
| Compound components with shared reactive state | Low - centralized state | Single reflow per input | Lower paint cost due to fewer updates | [OK] Good |