How to get component's HTML without mounting in Mithril.js

Published on in JavaScript and Mithril.js

Last updated on

Create a DocumentFragment and render the component to it.

Then there's no need to mount the component to the DOM.

const App = {
view: () => m('.foo', 'bar'),

const fragment = new DocumentFragment()
m.render(fragment, m(App))

//=> Element object

//=> Just the HTML: `<div class="foo">bar</div>`

Test the code on

DocumentFragment on MDN