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

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

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