Render functions let you create Vue components using JavaScript instead of templates. They give you more control and flexibility when building your UI.
Why render functions exist in Vue
export default { render() { return h('tag', { props }, children) } }
The render function returns virtual DOM nodes created with h().
You write JavaScript inside render() instead of using template HTML.
div with text.export default { render() { return h('div', 'Hello world') } }
export default { render() { return h('button', { onClick: () => alert('Clicked!') }, 'Click me') } }
This Vue component uses a render function to create a section with a heading and paragraph. It shows how you can build nested elements with JavaScript.
import { h, defineComponent } from 'vue' export default defineComponent({ render() { return h('section', { style: { padding: '1rem', border: '1px solid #ccc' } }, [ h('h1', 'Welcome'), h('p', 'This is rendered using a render function.') ]) } })
Render functions are more flexible but can be harder to read than templates.
Use render functions when templates are not enough for your UI needs.
Vue's h() helper creates virtual DOM nodes inside render functions.
Render functions let you build Vue components with JavaScript instead of templates.
They are useful for dynamic or complex UI that templates can't easily express.
Use h() to create elements inside the render function.
Practice
Solution
Step 1: Understand the purpose of render functions
Render functions allow building UI with JavaScript, useful when templates are limited.Step 2: Compare templates and render functions
Templates are simpler but less flexible; render functions handle complex dynamic UI better.Final Answer:
To create dynamic or complex UI that templates can't easily express -> Option CQuick Check:
Render functions = dynamic UI [OK]
- Thinking templates are deprecated
- Believing render functions avoid JavaScript
- Assuming render functions always boost performance
Solution
Step 1: Recall Vue 3 render function syntax
Vue 3 uses theh()function to create elements in render functions.Step 2: Identify correct usage
h('div', 'Hello World')correctly creates a div with text content.Final Answer:
return h('div', 'Hello World') -> Option AQuick Check:
Use h() to create elements [OK]
- Using JSX syntax without setup
- Using createElement which is Vue 2 style
- Using a non-existent render() function
render() {
return h('ul', [
h('li', 'Apple'),
h('li', 'Banana'),
h('li', 'Cherry')
])
}Solution
Step 1: Analyze the render function structure
The function returns aulelement with threelichildren created byh().Step 2: Understand the output HTML
The output is aulcontaining three list items with text 'Apple', 'Banana', and 'Cherry'.Final Answer:
<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul> -> Option DQuick Check:
h() creates nested elements correctly [OK]
- Thinking children concatenate as text
- Expecting multiple root elements without wrapper
- Confusing output with error about root element
render() {
return h('div', [
h('p', 'Hello'),
h('span', 'World')
])
}Solution
Step 1: Check h() usage
Passing an array of children as the second argument toh()is valid in Vue 3.Step 2: Verify root element presence
The root element is adiv, which wraps the children correctly.Final Answer:
No error, this render function is correct -> Option BQuick Check:
Array children inside h() are valid [OK]
- Thinking array children are invalid
- Expecting multiple root elements error here
- Assuming h() needs 3 arguments always
render() {
return h('ul', this.items.map(item =>
h('li', { class: item.selected ? 'active' : '' }, item.name)
))
}Solution
Step 1: Understand class binding in render functions
Passing an object with a 'class' property works to set classes conditionally.Step 2: Analyze the conditional expression
The ternary operator sets 'active' ifitem.selectedis true, else empty string, which means no class.Final Answer:
This code correctly applies 'active' class only to selected items -> Option AQuick Check:
Conditional class with ternary in render function works [OK]
- Using 'className' instead of 'class' in Vue
- Thinking class must be an array always
- Believing conditional classes are impossible in render functions
Ready to prove it?
Pass the test and this pattern moves unknown
