Skip to content
Mithril.js 2
Main Navigation GuideAPI

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

English

简体中文
繁體中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

Appearance

Sidebar Navigation

Getting Started

Installation

Tutorial

Resources

JSX

ES6+ on legacy browsers

Animation

Testing

Examples

3rd Party Integration

Path Handling

Key concepts

Vnodes

Components

Lifecycle methods

Keys

Autoredraw system

Misc

Framework comparison

Migrating from v1.x

Migrating from v0.2.x

API

On this page

3rd Party Integration ​

Integration with third party libraries or vanilla JavaScript code can be achieved via lifecycle methods.

noUiSlider Example ​

javascript
/** NoUiSlider wrapper component */
function Slider() {
  var slider;

  return {
    oncreate: function (vnode) {
      // Initialize 3rd party lib here
      slider = noUiSlider.create(vnode.dom, {
        start: 0,
        range: { min: 0, max: 100 },
      });
      slider.on('update', function (values) {
        vnode.attrs.onChange(values[0]);
        m.redraw();
      });
    },
    onremove: function () {
      // Cleanup 3rd party lib on removal
      slider.destroy();
    },
    view: function () {
      return m('div');
    },
  };
}

/** Demo app component */
function Demo() {
  var showSlider = false;
  var value = 0;

  return {
    view: function () {
      return m(
        '.app',
        m(
          'p',
          m(
            'button',
            {
              type: 'button',
              onclick: function () {
                showSlider = !showSlider;
              },
            },
            showSlider ? 'Destroy Slider' : 'Create Slider'
          )
        ),
        showSlider &&
          m(Slider, {
            onChange: function (v) {
              value = v;
            },
          }),
        m('p', value)
      );
    },
  };
}

m.mount(document.body, Demo);

Live Demo

Bootstrap FullCalendar Example ​

javascript
/** FullCalendar wrapper component */
var FullCalendar = {
  oncreate: function (vnode) {
    console.log('FullCalendar::oncreate');
    $(vnode.dom).fullCalendar({
      // put your initial options and callbacks here
    });
  },
  onremove: function (vnode) {
    // Run any destroy / cleanup methods here.
    $(vnode.dom).fullCalendar('destroy');
  },
  view: function (vnode) {
    return m('div');
  },
};

/** Demo app component */
function Demo() {
  var fullCalendarEl;

  function next() {
    $(fullCalendarEl).fullCalendar('next');
  }

  function prev() {
    $(fullCalendarEl).fullCalendar('prev');
  }

  return {
    view: function (vnode) {
      return [
        m('h1', 'Calendar'),
        m(FullCalendar, {
          oncreate: function (vnode) {
            fullCalendarEl = vnode.dom;
          },
        }),
        m(
          'button',
          {
            onclick: prev,
          },
          'Mithril.js Button -'
        ),
        m(
          'button',
          {
            onclick: next,
          },
          'Mithril.js Button +'
        ),
      ];
    },
  };
}

m.mount(document.body, Demo);

Live Demo

Pager
Previous pageExamples
Next pagePath Handling

Released under the MIT License.

Copyright (c) 2024 Mithril Contributors

https://mithril.js.org/integrating-libs.html

Released under the MIT License.

Copyright (c) 2024 Mithril Contributors