Mithril.js

2.3.8 · active · verified Sun Apr 19

Mithril.js is a modern, small, and fast client-side JavaScript framework primarily designed for building Single Page Applications. Currently at stable version 2.3.8, it maintains an active release cadence with regular patch updates and community-driven maintenance. Its key differentiators include a remarkably small bundle size (around 8.8 KiB gzipped), a highly performant virtual DOM diffing system, and built-in functionalities for routing and XHR requests, reducing the need for external libraries. Unlike some larger frameworks, Mithril.js focuses on providing core UI primitives without dictating an entire ecosystem, allowing developers more flexibility. It supports modern browsers (IE11, Firefox ESR, and the last two versions of Firefox, Edge, Safari, and Chrome) without requiring polyfills and can be used with or without build tools, although bundlers are recommended for production environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Mithril.js component with local state and event handling, then mounts it to the document body. It highlights how to define components, manage state, and render dynamic content.

import m from 'mithril';

interface MyComponentAttrs {
  name: string;
}

const MyComponent = {
  count: 0, // Component local state
  oninit: function(vnode: m.Vnode<MyComponentAttrs>) {
    // Initialize state, cannot assign directly to vnode.state in v2+
    vnode.state.count = 0;
  },
  view: function(vnode: m.Vnode<MyComponentAttrs>) {
    return m('div', [
      m('h1', `Hello, ${vnode.attrs.name}! Current count: ${vnode.state.count}`),
      m('button', {
        onclick: () => {
          vnode.state.count++;
        }
      }, 'Increment'),
      m('p', 'Click the button to increment the counter.')
    ]);
  }
};

m.mount(document.body, { view: () => m(MyComponent, { name: 'Mithril' }) });

// To demonstrate routing (requires an element to mount routes to, e.g., <div id="app"></div>)
// const homeRoute = { view: () => m('h2', 'Welcome Home!') };
// const aboutRoute = { view: () => m('h2', 'About Us') };
// m.route(document.getElementById('app') || document.body, '/home', {
//   '/home': homeRoute,
//   '/about': aboutRoute
// });

view raw JSON →