In Vue, templates are common for defining UI. Why might a developer choose to use render functions instead?
Think about what templates can and cannot do compared to JavaScript code.
Render functions let developers write UI using JavaScript directly, enabling dynamic logic and flexibility that templates cannot easily express.
Consider a Vue component defined with a render function instead of a template. What is the main difference in how Vue processes this component?
Think about how Vue creates the UI from render functions versus templates.
Vue uses render functions to create virtual DOM nodes directly at runtime, bypassing template compilation.
Which of the following render functions correctly creates a <div> with text 'Hello' in Vue 3?
import { h } from 'vue'; export default { render() { // Fill in the correct code here } }
The h function takes tag, props, and children as arguments.
In Vue 3, h('div', {}, 'Hello') correctly creates a div with text child 'Hello'. When props are specified (even empty {}), children must be passed as the third argument.
Given this Vue render function, why does it cause a runtime error?
import { h } from 'vue'; export default { render() { return h('div', { onclick: () => alert('Clicked!') }, 'Click me'); } }
Check how Vue expects event listeners to be named in render functions.
Vue expects event listeners in render functions to use camelCase ('onClick'), not all lowercase ('onclick').
When using render functions in Vue components, how do lifecycle hooks behave compared to template-based components?
Think about what lifecycle hooks depend on in Vue components.
Lifecycle hooks are part of the component instance and run normally whether the component uses templates or render functions.