Learn About HyperApp Virtual DOM and Virtual Nodes – HyperApp Concepts

HyperApp Virtual DOM

In the last article, we discussed about the Hyperapp installation overview, along with a basic example where we explained the concept of View, Action, and State used in building Hyperapp application. In this article, we are going to explore about the virtual DOM (Document Object Model) concept in HyperApp also known as Virtual Node.

Virtual DOM

A Virtual DOM is nothing but the description of a DOM structure by using a tree of JavaScript nested objects i.e. Virtual Nodes. Every render cycle using HyperApp creates the virtual DOM treefrom scratch. Therefore, the view function can be called every time when the state of application gets changed and a newly generated tree can update the actual DOM on each render cycle. In HyperApp, a tree changes in few DOM operations after comparing the new virtual DOM with the last created DOM and as a result it leads to high efficiency because only few parts of nodes are required to change instead of recalculating the entire virtual DOM.

HyperApp API has h function which helps in the creation of virtual nodes in a very efficient way. Alternatively, virtual nodes can be created with JSX. JSX is nothing but the JavaScript language extension which is used to represent dynamic HTML. We need to compile JSX into h function calls by importing h statement as browsers do not understand JSX directly. In the following example, we are going to describe about this process using babel.

Getting Started with Hyperapp

Step 1: – First of all, we need to install all dependencies after specifying them in “package. json” file. Let’s define the project name as “hyperapp-virtual-dom”. We can install the required dependencies under the “node_modules” of the current project after creating a file “package. json” with the below details. It should be noted that this file should be present at the project root where we are going to execute the “npm install” command. The “npm install” command will download and install all the required dependencies version present in the “package. json” file as shown below.

Package. json

Step 2: – Next, important file is “. eslintrc.js” and the content of this file is shown below. In this file, we have specified the required modules to bring up the Node.js application. In the current, Hyperapp JS application, we require below module exports.

Step 3: – Next, we need to create a “. babelrc” file. Below is the file content. Here, we are defining required plugins and presents.

Step 4: – Next important file is “webpack.config.js” and below is the content of this file. In this file, we have specified the vendor libraries, path, plugins, and module export which we are going to use in this hyperapp application.

Step 5: – Building Hyperapp view through Virtual DOM
In this example, we are going to use the virtual DOM where the view function is called every time we change the application state. The view function has the code logic for Virtual DOM and it get changes each time the application state changes as a result of the increase or decrease of the counter value (i.e. Basic +- counter computing application). HyperApp each time takes care to update DOM with greater efficiency by just replacing the parts of the DOM which are changed instead of the entire DOM. Given below is the detailed code logic for the above app implementation.
Here, we are defining the actions to increment or decrement the counter.

Here, the actual HyperApp code is defined. It decides the view of the application depending on the state and actions. This code is responsible for altering the view components and generating the virtual DOM to render using HyperApp.

Here, we have defined an application state.

This is our entire application build which collaborates between state, actions and view.

Below are the styling specifications for the app.

It is the starting point where we will be calling our app which we have just built.

Command to execute Hyperapp application
We need to execute “npm start” (as defined in “package. json” under scripts) at the project root of Hyperapp web application as shown below.
Hyperapp web application root
Once the application is built successfully, we can see the application loaded at below URL [http:/localhost:8080/] as shown below.
V Dom 6

Source code for Hyperapp concept

Conclusion: –
In this article, we discussed about a Hyperapp concept with Virtual DOM or Nodes along with a suitable example.


Please enter your comment!
Please enter your name here