Mithril.js: Get component's HTML without mounting

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))

console.log(fragment.firstChild)
//=> Element object

console.log(fragment.firstChild.outerHTML)
//=> Just the HTML: `<div class="foo">bar</div>`

Test the code on flems.io

DocumentFragment on MDN