Bird
Raised Fist0
Vueframework~20 mins

h function for creating vnodes in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue h Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component using h?

Consider this Vue 3 component using the h function to create virtual nodes:

import { h } from 'vue';

export default {
  setup() {
    return () => h('div', { class: 'container' }, [
      h('h1', 'Hello World'),
      h('p', 'This is a paragraph.')
    ]);
  }
};

What will this component render in the browser?

Vue
import { h } from 'vue';

export default {
  setup() {
    return () => h('div', { class: 'container' }, [
      h('h1', 'Hello World'),
      h('p', 'This is a paragraph.')
    ]);
  }
};
A<div class="container">Hello World<p>This is a paragraph.</p></div>
B<div><h1>Hello World</h1><p>This is a paragraph.</p></div>
C<div class="container"><h1>Hello World</h1><p>This is a paragraph.</p></div>
D<div class="container"><h1></h1><p></p></div>
Attempts:
2 left
💡 Hint

Remember that the second argument to h can be an object for attributes, and the third argument is children.

📝 Syntax
intermediate
2:00remaining
Which h function call correctly creates a button with a click event listener?

In Vue 3, you want to create a button element with the text 'Click me' and a click event that calls handleClick. Which h call is correct?

Ah('button', { onClick: handleClick }, 'Click me')
Bh('button', { onclick: handleClick }, 'Click me')
Ch('button', { onClick: handleClick() }, 'Click me')
Dh('button', { onClick: () => handleClick() }, 'Click me')
Attempts:
2 left
💡 Hint

Vue event listeners use on prefix with camelCase and expect a function reference, not a call.

🔧 Debug
advanced
2:00remaining
Why does this h function call cause a runtime error?

Examine this code snippet:

h('ul', null, [
  h('li', 'Item 1'),
  h('li', 'Item 2'),
  h('li')
])

Why does this cause a runtime error in Vue?

Vue
h('ul', null, [
  h('li', 'Item 1'),
  h('li', 'Item 2'),
  h('li')
])
AThe second argument <code>null</code> is invalid and should be an object.
BThe last <code>h('li')</code> call is missing children, causing undefined rendering error.
CThe array of children must be a flat array, nested arrays cause errors.
DThe <code>h</code> function requires at least two arguments; three is invalid.
Attempts:
2 left
💡 Hint

Check what happens if you call h with only one argument for an element that expects children.

🧠 Conceptual
advanced
2:00remaining
What is the main purpose of Vue's h function?

Choose the best description of what the h function does in Vue 3.

AIt creates virtual DOM nodes (vnodes) that represent elements before rendering.
BIt directly manipulates the real DOM elements for faster updates.
CIt compiles Vue templates into JavaScript render functions.
DIt manages component lifecycle hooks automatically.
Attempts:
2 left
💡 Hint

Think about what virtual DOM means and how Vue renders components.

❓ state_output
expert
2:00remaining
What is the output after clicking the button in this Vue component using h?

Consider this Vue 3 component:

import { h, ref } from 'vue';

export default {
  setup() {
    const count = ref(0);
    const increment = () => count.value++;
    return () => h('div', [
      h('button', { onClick: increment }, `Count: ${count.value}`)
    ]);
  }
};

What happens when you click the button?

Vue
import { h, ref } from 'vue';

export default {
  setup() {
    const count = ref(0);
    const increment = () => count.value++;
    return () => h('div', [
      h('button', { onClick: increment }, `Count: ${count.value}`)
    ]);
  }
};
AThe button label never updates; it stays at 'Count: 0' despite clicks.
BThe button label updates to show the incremented count each click.
CClicking the button causes a runtime error due to missing reactivity.
DThe count increments but the button label shows the previous count value.
Attempts:
2 left
💡 Hint

Consider how Vue tracks reactive changes and when the render function runs.

Practice

(1/5)
1. What is the primary purpose of the h function in Vue?
easy
A. To define CSS styles for components
B. To directly manipulate the DOM elements
C. To fetch data from an API
D. To create virtual nodes representing elements or components

Solution

  1. Step 1: Understand the role of h in Vue

    The h function is used to create virtual nodes (vnodes) which represent elements or components in Vue's virtual DOM.
  2. 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.
  3. Final Answer:

    To create virtual nodes representing elements or components -> Option D
  4. Quick Check:

    h function = create virtual nodes [OK]
Hint: Remember: h builds virtual nodes, not real DOM [OK]
Common Mistakes:
  • Confusing h with direct DOM manipulation
  • Thinking h fetches data
  • Assuming h styles components
2. Which of the following is the correct syntax to create a div element with the h function and a class of "container"?
easy
A. h('div', ['class', 'container'])
B. h('div', { class: 'container' })
C. h('div', 'class', 'container')
D. h('div', { 'className': 'container' })

Solution

  1. Step 1: Recall the h function signature

    The h function takes the tag name as the first argument and an object of props as the second argument.
  2. Step 2: Identify correct props syntax

    Vue uses class (not className) as the key for CSS classes in the props object.
  3. Final Answer:

    h('div', { class: 'container' }) -> Option B
  4. Quick Check:

    h(tag, props) with class key = A [OK]
Hint: Props are an object; use { class: 'name' } [OK]
Common Mistakes:
  • Using 'className' instead of 'class'
  • Passing class as separate string arguments
  • Using array instead of object for props
3. What will be the rendered output of this Vue render function snippet?
return h('ul', null, [
  h('li', null, 'Apple'),
  h('li', null, 'Banana'),
  h('li', null, 'Cherry')
])
medium
A.
  • Apple
  • Banana
  • Cherry
B.
    AppleBananaCherry
C.
  • Apple
  • Banana
  • Cherry
  • D. Error: Children must be strings

    Solution

    1. Step 1: Understand the structure created by h

      The outer h creates a ul element with an array of li children nodes.
    2. Step 2: Recognize the children rendering

      Each li vnode contains a string child representing the fruit name, so the final HTML is a list with three li items.
    3. Final Answer:

      <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul> -> Option A
    4. Quick Check:

      h with array children = nested elements [OK]
    Hint: Array children create nested elements inside parent [OK]
    Common Mistakes:
    • Thinking children array concatenates text
    • Expecting error for array children
    • Missing the parent <ul> element
    4. Identify the error in this usage of the h function:
    return h('button', { onclick: () => alert('Clicked!') }, 'Click me')
    medium
    A. The event name should be lowercase: 'onclick'
    B. The event handler should be passed as 'onClick' but as a string
    C. The event handler should be passed as 'onClick' with capital C
    D. The event handler should be passed as 'onClick' but inside an array

    Solution

    1. Step 1: Recall Vue event listener naming in h

      Vue expects event listeners in props to be prefixed with 'on' and camelCase event name, e.g., 'onClick'.
    2. Step 2: Identify the incorrect casing

      The code uses 'onclick' with lowercase 'c', which Vue does not recognize as an event listener.
    3. Final Answer:

      The event handler should be passed as 'onClick' with capital C -> Option C
    4. Quick Check:

      Event props use camelCase event names [OK]
    Hint: Use camelCase event names like 'onClick' in props [OK]
    Common Mistakes:
    • Using lowercase event names
    • Passing event handler as string
    • Wrapping handler in array unnecessarily
    5. How would you use the 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']
    hard
    A. h('ul', null, fruits.filter(f => f.length > 5).map(f => h('li', null, f)))
    B. h('ul', null, fruits.map(f => f.length > 5 ? h('li', null, f) : f))
    C. h('ul', null, fruits.map(f => h('li', null, f.length > 5)))
    D. h('ul', null, fruits.filter(f => f.length > 5))

    Solution

    1. Step 1: Filter fruits by length

      Use filter to keep only fruits with names longer than 5 characters.
    2. Step 2: Map filtered fruits to li vnodes

      Use map to convert each filtered fruit into an li vnode with the fruit name as child.
    3. Step 3: Pass the array of li vnodes as children to ul

      This creates a ul with only the desired fruits as list items.
    4. Final Answer:

      h('ul', null, fruits.filter(f => f.length > 5).map(f => h('li', null, f))) -> Option A
    5. Quick Check:

      Filter then map to
    6. nodes = A [OK]
    Hint: Filter first, then map to <li> nodes inside h [OK]
    Common Mistakes:
    • Mapping without filtering includes unwanted items
    • Passing booleans or nulls as children
    • Passing raw strings array without wrapping in <li>
    Recommended

    Ready to prove it?

    Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

    10 questions~5 mineasy hard