Complete the code to create a vnode for a <div> element using the h function.
const vnode = h([1]);The h function creates a vnode for the specified HTML tag. Here, 'div' creates a
Complete the code to create a vnode for a <button> element with a click event handler using the h function.
const vnode = h('button', { [1]: () => alert('Clicked!') }, 'Click me');
onclick instead of camelCase onClick.onClickEvent.In Vue's h function, event listeners use camelCase with on prefix, so onClick is correct.
Fix the error in the code to create a vnode with children using the h function.
const vnode = h('ul', null, [[1]]);
Keys should be unique and are typically strings. Using { key: '1' } is correct and helps Vue track list items.
Fill both blanks to create a vnode for a <p> element with a class and text content using the h function.
const vnode = h('p', { [1]: 'text-primary' }, [2]);
style instead of class for CSS classes.The class property sets the CSS class, and the third argument is the text content.
Fill all three blanks to create a vnode for a <section> with an id, inline style, and two child <p> elements using the h function.
const vnode = h('section', { [1]: 'main-section', [2]: { color: 'blue' } }, [h('p', null, [3]), h('p', null, 'Second paragraph')]);
class instead of id for the id attribute.element.
The id sets the element's id, style sets inline styles as an object, and the third blank is the text for the first child paragraph.
Practice
h function in Vue?Solution
Step 1: Understand the role of
Thehin Vuehfunction is used to create virtual nodes (vnodes) which represent elements or components in Vue's virtual DOM.Step 2: Differentiate from other functions
It does not manipulate the real DOM directly, fetch data, or style components. Its main role is to build the virtual DOM structure.Final Answer:
To create virtual nodes representing elements or components -> Option DQuick Check:
h function = create virtual nodes [OK]
- Confusing h with direct DOM manipulation
- Thinking h fetches data
- Assuming h styles components
div element with the h function and a class of "container"?Solution
Step 1: Recall the
Thehfunction signaturehfunction takes the tag name as the first argument and an object of props as the second argument.Step 2: Identify correct props syntax
Vue usesclass(notclassName) as the key for CSS classes in the props object.Final Answer:
h('div', { class: 'container' }) -> Option BQuick Check:
h(tag, props) with class key = A [OK]
- Using 'className' instead of 'class'
- Passing class as separate string arguments
- Using array instead of object for props
return h('ul', null, [
h('li', null, 'Apple'),
h('li', null, 'Banana'),
h('li', null, 'Cherry')
])Solution
Step 1: Understand the structure created by
The outerhhcreates aulelement with an array oflichildren nodes.Step 2: Recognize the children rendering
Eachlivnode contains a string child representing the fruit name, so the final HTML is a list with threeliitems.Final Answer:
<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul> -> Option AQuick Check:
h with array children = nested elements [OK]
- Thinking children array concatenates text
- Expecting error for array children
- Missing the parent <ul> element
h function:
return h('button', { onclick: () => alert('Clicked!') }, 'Click me')Solution
Step 1: Recall Vue event listener naming in
Vue expects event listeners in props to be prefixed with 'on' and camelCase event name, e.g., 'onClick'.hStep 2: Identify the incorrect casing
The code uses 'onclick' with lowercase 'c', which Vue does not recognize as an event listener.Final Answer:
The event handler should be passed as 'onClick' with capital C -> Option CQuick Check:
Event props use camelCase event names [OK]
- Using lowercase event names
- Passing event handler as string
- Wrapping handler in array unnecessarily
h function to create a ul list from this array of fruits, but only include fruits with names longer than 5 characters?
const fruits = ['Apple', 'Banana', 'Fig', 'Cherry']
Solution
Step 1: Filter fruits by length
Usefilterto keep only fruits with names longer than 5 characters.Step 2: Map filtered fruits to
Uselivnodesmapto convert each filtered fruit into anlivnode with the fruit name as child.Step 3: Pass the array of
This creates alivnodes as children toululwith only the desired fruits as list items.Final Answer:
h('ul', null, fruits.filter(f => f.length > 5).map(f => h('li', null, f))) -> Option AQuick Check:
Filter then map to- nodes = A [OK]
- Mapping without filtering includes unwanted items
- Passing booleans or nulls as children
- Passing raw strings array without wrapping in <li>
Ready to prove it?
Pass the test and this pattern moves unknown
