.agents/skills/vue-best-practices/references/render-functions.md
Impact: MEDIUM - Render functions are powerful but opt out of template compiler optimizations. Use them intentionally and apply the key patterns below to keep output correct and performant.
h()/JSXwithModifiers / withKeys for event modifiersv-model via modelValue + onUpdate:modelValuewithDirectivesBAD:
<script setup>
import { h, ref } from 'vue'
const count = ref(0)
const render = () => h('div', `Count: ${count.value}`)
</script>
GOOD:
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
<template>
<div>Count: {{ count }}</div>
</template>
BAD:
import { h, ref } from 'vue'
export default {
setup() {
const items = ref([{ id: 1, name: 'Apple' }])
return () => h('ul', items.value.map(item => h('li', item.name)))
}
}
GOOD:
import { h, ref } from 'vue'
export default {
setup() {
const items = ref([{ id: 1, name: 'Apple' }])
return () => h('ul', items.value.map(item => h('li', { key: item.id }, item.name)))
}
}
withModifiers / withKeys for event modifiersBAD:
import { h } from 'vue'
export default {
setup() {
const handleClick = (e) => {
e.stopPropagation()
e.preventDefault()
}
return () => h('button', { onClick: handleClick }, 'Click')
}
}
GOOD:
import { h, withKeys, withModifiers } from 'vue'
export default {
setup() {
const handleClick = () => {}
const handleEnter = () => {}
return () => h('div', [
h('button', {
onClick: withModifiers(handleClick, ['stop', 'prevent'])
}, 'Click'),
h('input', {
onKeyup: withKeys(handleEnter, ['enter'])
})
])
}
}
v-model explicitlyBAD:
import { h, ref } from 'vue'
import CustomInput from './CustomInput.vue'
export default {
setup() {
const text = ref('')
return () => h(CustomInput, { modelValue: text.value })
}
}
GOOD:
import { h, ref } from 'vue'
import CustomInput from './CustomInput.vue'
export default {
setup() {
const text = ref('')
return () => h(CustomInput, {
'modelValue': text.value,
'onUpdate:modelValue': (value) => { text.value = value }
})
}
}
withDirectives for custom directivesBAD:
import { h } from 'vue'
const vFocus = { mounted: el => el.focus() }
export default {
setup() {
return () => h('input', { 'v-focus': true })
}
}
GOOD:
import { h, withDirectives } from 'vue'
const vFocus = { mounted: el => el.focus() }
export default {
setup() {
return () => withDirectives(h('input'), [[vFocus]])
}
}
BAD:
import { h } from 'vue'
export default {
setup() {
return () => h('span', { class: 'badge' }, 'New')
}
}
GOOD:
import { h } from 'vue'
function Badge(props, { slots }) {
return h('span', { class: 'badge' }, slots.default?.())
}
Badge.props = ['variant']
export default Badge