Renders a template to the DOM
m.render(document.body, "hello") // <body>hello</body>
m.render(element, vnodes, redraw)
||Yes||A DOM element that will be the parent node to the subtree|
||Yes||The vnodes to be rendered|
||No||A callback invoked each time an event handler in the subtree is invoked|
How to read signatures
How it works
m.render(element, vnodes) method takes a virtual DOM tree (typically generated via the
m() hyperscript function), generates a DOM tree and mounts it on
element already has a DOM tree mounted via a previous
vnodes is diffed against the previous
vnodes tree and the existing DOM tree is modified only where needed to reflect the changes. Unchanged DOM nodes are not touched at all.
If you pass the optional
redraw argument, that is invoked each time an event handler anywhere in the subtree is called. This is used by
m.redraw to implement the autoredraw functionality, but it's also exposed for more advanced use cases like integration with some third-party frameworks.
m.render is synchronous.
Why Virtual DOM
Touching the DOM can be extremely expensive for a couple of reasons. Alternating reads and writes can adversely affect performance by causing several browser repaints to occur in quick succession, whereas comparing virtual dom trees allows writes to be batched into a single repaint. Also, the performance characteristics of various DOM operations vary between implementations and can be difficult to learn and optimize for all browsers. For example, in some implementations, reading
childNodes.length has a complexity of O(n); in some, reading
parentNode causes a repaint, etc.
Differences from other API methods
m.render() method is internally called by
m.request(). It is not called after stream updates
m.route(), a vnode tree rendered via
m.render() does not auto-redraw in response to view events,
m.redraw() calls or
m.request() calls. It is a low level mechanism suitable for library authors who wish to manually control rendering instead of relying on Mithril.js' built-in auto-redrawing system.
Another difference is that
m.render method expects a vnode (or a array of vnodes) as its second parameter, whereas
m.route() expect components.
var render = require("mithril/render")
m.render module is similar in scope to view libraries like Knockout, React and Vue. It implements a virtual DOM diffing engine with a modern search space reduction algorithm and DOM recycling, which translate to top-of-class performance, both in terms of initial page load and re-rendering. It has no dependencies on other parts of Mithril.js aside from normalization exposed via
require("mithril/render/vnode") and can be used as a standalone library.
Despite being relatively small, the render module is fully functional and self-sufficient. It supports everything you might expect: SVG, custom elements, and all valid attributes and events - without any weird case-sensitive edge cases or exceptions. Of course, it also fully supports components and lifecycle methods.
m.render as a standalone module, it may be useful to also import the
hyperscript function to create the vnodes to be rendered.
License: MIT. © Leo Horie.