Trying out MithrilJS
These days, when you start a new project and want to pick a tool for front-end development, there are 3 options that most people will go for: react, Angular and Vue.
I’m not really good at front-end development. I have only worked with Angular for a short time, and it has always been my go-to framework at front-end. As I don’t have experience with react or Vue, I cannot make a comparison. But from my point of view, the good things about Angular are:
- It’s a full framework, so very feature rich
- Good documentation
- Pretty popular, can find resources easily
In general, I think Angular is great for big application, but overkill for small hobby projects. Recently, with an idea for a new project, I started with Angular as usual. After a few days, I was wondering if I could find an alternative because Angular was too big/complicated for my needs.
I also wanted to try something new, simple, easy to learn, does not need a bunch of tools to setup/build/run. That’s when I remembered about Mithril. A few years ago I saw a discussion on a tech forum, but I wasn’t interested at the time. And now with 4-day weekend at home, I decided to try out Mithril. Let’s see how long it takes me to create a simple application, and how is it different from Angular.
What is Mithril?
From the homepage: Mithril is a modern client-side JavaScript framework for building Single Page Applications. It’s small (< 10kb gzip), fast and provides routing and XHR utilities out of the box.
It took me around 15 mins to go through the Getting started guide, and around 4 hours for the tutorial, which is pretty fast in my opinion.
OK, let’s jump to the development. The sample application this time is very simple, just a login screen, a dashboard, probably CRUD operations. The purpose of this project is to test Mithril, so it uses some non-best practices.
Installation
This is pretty simple, just follow the guide.
Initialize the directory as an npm package:
|
|
Install required tools:
|
|
Edit package.json
|
|
That’s all for the installation. Now I will create index.html
file.
|
|
It’s pretty straightforward, here I use Pure.css for easy styling.
Now I need an entry point file index.js
, this file works as a starting point for my application. This is where the routing is defined, but since I haven’t created any component, I just include Mithril in the module.
|
|
Mithril Components
Let’s create src
folder to store the application source code.
From the homepage, a Mithril component is simply an object that has a view method.
OK let’s create the components
folder, where our components are stored. Then create Login.js
file inside.
By default, Mithril views are described using hyperscript. There is also a tool to convert from HTML to Mithril template, Check out Converting HTML, super easy to use. JSX is also supported if you prefer HTML syntax. For now I just stick with hyperscript.
|
|
I will go back and update index.js
file.
|
|
Finally, create style.css
in the same folder with index.html
for styling.
|
|
Let’s test it, change to project directory, type the command below:
|
|
Final
That’s my accomplishment after 1 day into Mithril JS. I’m really impressed with the framework, it has simple API, small learning curve, doesn’t require a build step and the documentation is easy to understand. If you want to know what is the differences between Mithril and other popular frameworks, you can take a look at Framework comparison.
The only weakness I found with Mithril is the availability of learning resources. As far as I know, even though Mithril has been around for a number of years, it’s not really popular compared to react, Angular or Vue. Therefore, if you are stuck with some problems while working with Mithril, it could be a bit difficult to look for tutorials, articles, etc.
I will continue using Mithril in this project for a while, go over some more advanced features, and let see what are the other strengths and weaknesses.