Mithril.js is a modern client-side JavaScript framework for building Single Page Applications. It's small (8.8 KiB gzip), fast and provides routing and XHR utilities out of the box.
Mithril.js is a modern client-side JavaScript framework designed for building Single Page Applications (SPAs).
Here is a summary of its principal features and characteristics:
Performance and Utility
Size and Speed: Mithril.js is notably small (8.8 KiB gzip) and is recognized as being fast, with a performance time of 6.4 ms.
Built-in Utilities: It provides routing and XHR utilities out of the box.
Compatibility: The framework supports IE11, Firefox ESR, and the last two versions of Firefox, Edge, Safari, and Chrome, and no polyfills are required.
DOM Management and Rendering
HTML Structure Definition: The m() function is used to describe any desired HTML structure. This function can incorporate attributes (like {class: "title"}) and include multiple nested elements.
Creating and Updating HTML: The m.render() function is used both to create and update HTML.
Efficiency: Mithril.js automatically determines the most efficient way of updating the text instead of recreating it from scratch. When updates occur (e.g., via button clicks), it is very fast because it only touches the parts of the DOM it absolutely needs to.
Components and Interactivity
Components Definition: A Mithril.js component is defined as an object with a view function.
Auto-Redrawing System: Using m.mount() activates Mithril.js' auto-redrawing system. This system automatically applies changes (such as incrementing a variable via an event handler) to the HTML, eliminating the need to manually call m.render to apply changes.
Event Handling: Events, such as onclick, can be defined directly on elements within the component's view function.
Routing and Server Communication
Routing Functionality: Routing enables navigation from one screen to another within an application.
The m.route() function is used for applications with multiple screens.
It maintains the auto-redrawing functionality of m.mount.
It enables URL awareness, allowing the framework to know what to do when it sees a hashbang (#!) in the URL, which is a common convention for SPAs.
It allows setting a default route.
XHR (Server Interaction): XHR provides a way to talk to a server.
The m.request() function is used for server communication, allowing developers to specify the method (e.g., PUT), URL, and body payload.
The framework allows developers to update variables (like a count) using the response value from the server after the request completes.
Key Concepts
The framework involves several key concepts, including Vnodes, Components, Lifecycle methods, Keys, and the Autoredraw system.