Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
Vue 3.0 introduces an improved version of the render function API which makes it more intuitive and easier to use. The primary purpose of a render function is to programmatically declare the render output of a Vue component. This can be particularly useful when you're dealing with dynamic components or when the template syntax isn't powerful enough for your specific use case.
Let's dive in with a simple example:
In Vue 2, a basic render function looks like this:
export default { render(createElement) { return createElement('h1', 'Hello, Vue!') } }
In Vue 3, we use the h
function, short for hyperscript. The above render function translates to the following in Vue 3:
import { h } from 'vue' export default { render() { return h('h1', 'Hello, Vue!') } }
Render functions can be particularly useful when you want to render components or HTML elements dynamically. Here's an example where we render different heading elements based on a component's property:
import { h } from 'vue' export default { props: { level: { type: Number, required: true } }, render() { return h(`h${this.level}`, `This is an h${this.level} heading.`) } }
In this example, you can bind the level
prop to change the type of heading element that gets rendered.
You can also use render functions to loop over an array of items and render a component for each item. Here's an example:
import { h } from 'vue' export default { data() { return { items: ['Apple', 'Banana', 'Cherry'] } }, render() { return h('ul', this.items.map(item => h('li', item))) } }
This will render an unordered list (ul
) with a list item (li
) for each item in the items
array.
Render functions give you more power and flexibility than templates, but they can be more complex and harder to read, so use them sparingly and only when necessary.
Vue 3.0 h()
Function in the Render API:
h()
function is short for createElement()
and is used to create virtual DOM nodes.import { createApp, h } from 'vue'; const app = createApp({ render() { return h('div', { class: 'container' }, 'Hello, Vue 3.0!'); }, }); app.mount('#app');
Composing Components with the Render Function in Vue 3.0:
h()
to compose components within the render function.import { createApp, h } from 'vue'; import MyComponent from './MyComponent.vue'; const app = createApp({ render() { return h('div', { class: 'container' }, [ h(MyComponent), h('p', 'Another element'), ]); }, }); app.mount('#app');
Dynamic Rendering in Vue 3.0 Using the Render Function:
import { createApp, h } from 'vue'; const app = createApp({ data() { return { dynamicTag: 'div', }; }, render() { return h(this.dynamicTag, { class: 'container' }, 'Dynamic Rendering'); }, }); app.mount('#app');
Vue 3.0 Render Function and Scoped Slots:
h()
to create components with scoped slots.import { createApp, h } from 'vue'; const app = createApp({ render() { return h('my-component', { scopedSlots: { default: (props) => h('p', props.message), }, }); }, }); app.mount('#app');
Vue 3.0 Render Function and Custom Directives:
import { createApp, h } from 'vue'; const app = createApp({ directives: { customDirective(el, binding) { el.style.color = binding.value; }, }, render() { return h('div', { class: 'container', customDirective: 'red', }, 'Custom Directive'); }, }); app.mount('#app');
Vue 3.0 Render Function and v-bind
/v-on
Usage:
v-bind
and v-on
within the render function.import { createApp, h } from 'vue'; const app = createApp({ data() { return { href: 'https://www.vuejs.org', onClick() { alert('Clicked!'); }, }; }, render() { return h('a', { href: this.href, onClick: this.onClick, }, 'Click me'); }, }); app.mount('#app');
Vue 3.0 Render Function and Functional Components:
import { createApp, h } from 'vue'; const FunctionalComponent = { functional: true, render(_, { props }) { return h('p', props.message); }, }; const app = createApp({ render() { return h(FunctionalComponent, { message: 'Functional Component' }); }, }); app.mount('#app');
Vue 3.0 Render Function and Reactivity:
import { createApp, h, reactive } from 'vue'; const app = createApp({ data() { return reactive({ message: 'Reactive Message', }); }, render() { return h('p', this.message); }, }); app.mount('#app');