Performance: Why render functions exist
Render functions affect how Vue builds and updates the DOM, impacting rendering speed and flexibility.
import { h } from 'vue'; export default { render() { return h(this.dynamicComponent); } }
<template>
<div>
<component :is="dynamicComponent" />
</div>
</template>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Template with runtime compiler | Moderate | Multiple on updates | Moderate | [!] OK |
| Pre-compiled template | Moderate | Multiple on updates | Moderate | [OK] Good |
| Render function | Moderate | Multiple on updates | Moderate | [OK] Good |
h() function to create elements in render functions.h('div', 'Hello World') correctly creates a div with text content.render() {
return h('ul', [
h('li', 'Apple'),
h('li', 'Banana'),
h('li', 'Cherry')
])
}ul element with three li children created by h().ul containing three list items with text 'Apple', 'Banana', and 'Cherry'.render() {
return h('div', [
h('p', 'Hello'),
h('span', 'World')
])
}h() is valid in Vue 3.div, which wraps the children correctly.render() {
return h('ul', this.items.map(item =>
h('li', { class: item.selected ? 'active' : '' }, item.name)
))
}item.selected is true, else empty string, which means no class.Pass the test and this pattern moves unknown