A render function is a JavaScript function that returns Virtual DOM nodes instead of using template syntax. It tells Vue how to build the UI programmatically.
Click to reveal answer
beginner
How do you create a simple render function in Vue 3?
You define a function that returns <code>h()</code> calls, where <code>h</code> is the createElement helper imported from Vue. For example: <pre>import { h } from 'vue';
export default { render() { return h('div', 'Hello!'); } }</pre>
Click to reveal answer
beginner
What does the h function do in Vue render functions?
The h function creates Virtual DOM nodes. It takes the tag name, props, and children as arguments to build the UI structure.
Click to reveal answer
intermediate
Why might you use a render function instead of a template in Vue?
Render functions give you more control and flexibility, especially for dynamic or complex UI logic that is hard to express in templates.
Click to reveal answer
intermediate
How do you pass props and children in a Vue render function?
You pass props as the second argument to h as an object, and children as the third argument as a string, array, or another h call. Example:
h('button', { disabled: true }, 'Click me')
Click to reveal answer
What does the h function in Vue render functions stand for?
AHook function for lifecycle
BHTML string generator
CHyperscript helper to create Virtual DOM nodes
DHelper for CSS styling
✗ Incorrect
The h function is short for 'hyperscript' and creates Virtual DOM nodes in Vue render functions.
Which of these is a correct minimal render function in Vue 3?
Hello } uses JSX which is not default Vue render syntax. render() { return h(div, 'Hello') } misses quotes around 'div'. render() { return h('div', { text: 'Hello' }) } incorrectly uses an object with a text property.
Final Answer:
render() { return h('div', 'Hello') } -> Option D
Quick Check:
Tag as string + children string = correct syntax [OK]
Hint: Tag names must be strings in h calls [OK]
Common Mistakes:
Omitting quotes around tag names
Using JSX syntax without setup
Passing text inside an object instead of as child
3. What will the following Vue render function output?
A. Event handler key should be 'onClick' with capital C
B. Event handler key should be 'onclick' all lowercase
C. Event handler key should be 'onClick' but Vue expects 'onClick' as camelCase
D. Event handler key should be 'onClick' but Vue expects 'onClick' as 'onClick'
Solution
Step 1: Recall Vue event handler naming in render functions
Vue expects event handlers in render functions to use camelCase keys prefixed with 'on', like 'onClick'.
Step 2: Identify the incorrect key
The code uses 'onclick' with lowercase 'c', which Vue does not recognize as an event handler.
Final Answer:
Event handler key should be 'onClick' with capital C -> Option A
Quick Check:
Vue event keys are camelCase 'onClick' [OK]
Hint: Use camelCase event keys like 'onClick' in render functions [OK]
Common Mistakes:
Using lowercase event keys instead of camelCase
Confusing Vue event keys with JSX syntax
Assuming 'onclick' works like HTML attributes
5. How can you use a Vue render function to conditionally render a <span> with text 'Active' only if a signal isActive is true, otherwise render <span> with 'Inactive'?
hard
A. render() { if (isActive) return h('span', 'Active'); else return h('span', 'Inactive') }