0
0
Vueframework~20 mins

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

Choose your learning style9 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.