Render functions allow you to write JavaScript code that creates elements dynamically, which is useful for complex or highly dynamic UI structures. Templates are simpler and better for static or declarative markup.
const MyComponent = { template: `<div><p>Hello from template</p></div>` } const MyRenderComponent = { render() { return Vue.h('div', [Vue.h('p', 'Hello from render function')]) } }
The template creates a div containing a paragraph with text. The render function uses Vue's h() helper to create the same structure programmatically. Both render a div with a paragraph inside.
render() {
return Vue.h('div', [
Vue.h('p', 'Line 1'),
Vue.h('p', 'Line 2')
])
}The array passed as the second argument to Vue.h is not properly closed. The closing square bracket is missing before the closing parenthesis.
In Vue render functions, event listeners use camelCase keys: 'onClick' (uppercase 'C') for the 'click' event. The code uses 'onclick' (lowercase), which Vue does not recognize as an event listener, so the handler is not bound.
Vue tracks reactive data like count. When increment updates count, Vue re-renders the component, updating the displayed count. After two clicks, count becomes 2 and the UI shows 'Count: 2'.