Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/rendering-render-function-h-import-vue3.md
1---2title: Import h Globally in Vue 3 Render Functions3impact: HIGH4impactDescription: Vue 3 requires explicit h import; using Vue 2 patterns causes runtime errors5type: gotcha6tags: [vue3, render-function, migration, h, vnode, breaking-change]7---89# Import h Globally in Vue 3 Render Functions1011**Impact: HIGH** - In Vue 2, the `h` function (createElement) was passed as an argument to render functions. In Vue 3, `h` must be explicitly imported from 'vue'. Using Vue 2 patterns causes runtime errors.1213## Task Checklist1415- [ ] Import `h` from 'vue' at the top of files using render functions16- [ ] Remove the `h` parameter from render function signatures17- [ ] Update all render functions when migrating from Vue 21819**Incorrect (Vue 2 pattern - broken in Vue 3):**20```js21// WRONG: Vue 2 pattern - h is not passed as argument in Vue 322export default {23render(h) { // h is undefined in Vue 3!24return h('div', [25h('span', 'Hello')26])27}28}2930// WRONG: Using createElement alias from Vue 231export default {32render(createElement) { // Also undefined33return createElement('div', 'Hello')34}35}36```3738**Correct (Vue 3 pattern):**39```js40// CORRECT: Import h from vue41import { h } from 'vue'4243export default {44render() {45return h('div', [46h('span', 'Hello')47])48}49}50```5152## With Composition API5354```js55import { h, ref } from 'vue'5657export default {58setup() {59const count = ref(0)6061// Return a render function from setup62return () => h('div', [63h('button', { onClick: () => count.value++ }, `Count: ${count.value}`)64])65}66}67```6869## With script setup (Not Recommended)7071```vue72<script setup>73import { h, ref } from 'vue'7475const count = ref(0)7677// Cannot return render function from script setup78// Must use a separate render option or template79</script>8081<!-- script setup typically uses templates, not render functions -->82<template>83<div>84<button @click="count++">Count: {{ count }}</button>85</div>86</template>87```8889If you need render functions with `<script setup>`, use the `render` option:9091```vue92<script>93import { h, ref } from 'vue'9495export default {96setup() {97const count = ref(0)98return () => h('button', { onClick: () => count.value++ }, count.value)99}100}101</script>102```103104## Component Resolution Change105106In Vue 3, you must also explicitly resolve components:107108**Incorrect:**109```js110// Vue 2: Could use string names for registered components111render(h) {112return h('my-component', { props: { value: 1 } })113}114```115116**Correct:**117```js118import { h, resolveComponent } from 'vue'119120export default {121render() {122// Must resolve component by name123const MyComponent = resolveComponent('my-component')124return h(MyComponent, { value: 1 })125}126}127128// Or import the component directly (preferred)129import { h } from 'vue'130import MyComponent from './MyComponent.vue'131132export default {133render() {134return h(MyComponent, { value: 1 })135}136}137```138139## Why This Changed140141Vue 3's `h` is globally importable to:1421. Enable tree-shaking (unused features can be removed)1432. Support better TypeScript inference1443. Allow use outside of component context145146## Reference147- [Vue 3 Migration Guide - Render Function API](https://v3-migration.vuejs.org/breaking-changes/render-function-api.html)148- [Vue.js Render Functions & JSX](https://vuejs.org/guide/extras/render-function.html)149