Render functions and templates both create the HTML you see on the page. Choosing between them helps you write clearer and easier-to-manage Vue components.
When you want simple and easy-to-read HTML structure, use templates.
When you need more control or dynamic logic in your HTML, use render functions.
When you want to write your component entirely in JavaScript, use render functions.
When you prefer a clear separation of HTML and JavaScript, use templates.
When you want to use JSX syntax inside Vue, use render functions.
Syntax
Vue
Template syntax:
<template>
<div>Hello, {{ name }}!</div>
</template>
Render function syntax:
import { h } from'vue'exportdefault {
render() {
return h('div', `Hello, ${this.name}!`)
}
}
Templates look like regular HTML with special Vue tags.
Render functions use JavaScript to create HTML elements.
Examples
This template example shows a button that updates a counter when clicked.
A. The render function must return a template string.
B. The h function cannot accept event handlers.
C. The 'div' tag is invalid in render functions.
D. The event name should be 'onClick' with capital C.
Solution
Step 1: Check event handler naming
In Vue render functions, event handlers use camelCase like 'onClick', not lowercase 'onclick'.
Step 2: Validate other parts
The h function accepts event handlers, and 'div' is a valid tag. Render functions return VNodes, not template strings.
Final Answer:
The event name should be 'onClick' with capital C. -> Option D
Quick Check:
Event names in render functions are camelCase [OK]
Hint: Use camelCase event names like onClick in render functions [OK]
Common Mistakes:
Using lowercase event names
Expecting templates inside render functions
Thinking h() can't handle events
5. You want to create a Vue component that conditionally renders a list of items with complex logic and dynamic keys. Which approach is best and why?
Options: A) Use templates with v-for and v-if. B) Use render functions for full JavaScript control. C) Use templates only, avoid render functions. D) Use plain JavaScript without Vue features.
hard
A. Templates with v-for and v-if are simpler and sufficient for most cases.
B. Templates are always better for any complexity.
C. Render functions allow complex logic and dynamic keys more flexibly.
D. Avoid Vue features and write plain JavaScript for control.
Solution
Step 1: Understand complexity needs
Complex logic and dynamic keys in lists can be hard to express cleanly in templates.
Step 2: Evaluate render functions benefits
Render functions use JavaScript directly, allowing more flexible and dynamic rendering control.
Step 3: Compare options
Templates are simpler but may be limited for complex dynamic keys. Plain JavaScript without Vue loses reactivity and benefits.
Final Answer:
Render functions allow complex logic and dynamic keys more flexibly. -> Option C
Quick Check:
Complex dynamic rendering = render functions [OK]
Hint: Use render functions for complex dynamic rendering needs [OK]
Common Mistakes:
Assuming templates handle all complex logic easily
Ignoring reactivity by avoiding Vue features
Thinking plain JavaScript is better than Vue for UI
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.