📖 Scenario: You are building a simple Vue 3 component that displays a greeting message using the h function to create virtual nodes (vnodes).
🎯 Goal: Build a Vue 3 component using the h function to create a <div> element containing a <h1> heading with the text "Hello, Vue!".
📋 What You'll Learn
Create a Vue 3 component using `defineComponent`.
Use the h function from Vue to create virtual nodes.
Render a <div> element as the root with an <h1> child containing the text "Hello, Vue!".
Do not use template syntax; use only the h function for rendering.
💡 Why This Matters
🌍 Real World
Using the h function is useful when you want to create Vue components programmatically without templates, such as in render functions or JSX alternatives.
💼 Career
Understanding how to create virtual nodes with the h function helps you build flexible Vue components and is a key skill for advanced Vue development and custom render logic.
Progress0 / 4 steps
1
Setup Vue Component and Import h
Create a Vue 3 component by importing defineComponent and h from 'vue'. Then define a component named HelloVue using defineComponent.
Vue
Hint
Start by importing defineComponent and h from 'vue'. Then create a component with defineComponent and give it the name HelloVue.
2
Add a Render Function Using h
Inside the HelloVue component, add a render function that returns a virtual node created by h. For now, return a <div> element with no children.
Vue
Hint
Define a render method that returns h('div') to create an empty <div> element.
3
Add an <h1> Child with Text
Modify the render function to return a <div> element with one child: an <h1> element containing the text "Hello, Vue!". Use h to create both elements and nest the <h1> inside the <div>.
Vue
Hint
Use h('div', [h('h1', 'Hello, Vue!')]) to create a <div> with an <h1> child containing the text.
4
Export the Component as Default
Ensure the HelloVue component is exported as the default export so it can be used in a Vue app.
Vue
Hint
Make sure the component is exported with export default defineComponent.
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
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.
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 D
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
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.
Step 2: Identify correct props syntax
Vue uses class (not className) as the key for CSS classes in the props object.
Final Answer:
h('div', { class: 'container' }) -> Option B
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?