Vnode Examples of Passing Variables In

by osban

Level: beginner • Mithril.js Version: latest

This is an example that shows how to pass variables into a component using Vnode. The first variant is using vnode itself, while the second one is using the same, but with a shorter variable name. More interesting are the other two, that are using destructuring assignment JavaScript expressions, the latter one even with nested destructuring.

Live Example


Type Name URL


const full   = {view: vnode => m('h1', vnode.attrs.test)}
const short  = {view: v => m('h1', v.attrs.test)}
const dest   = {view: ({attrs}) => m('h1', attrs.test)}
const destf  = {view: ({attrs: {test}}) => m('h1', test)}

m.mount(document.body, {
  view: () => [
    m(full,  {test: "full"}),
    m(short, {test: "short"}),
    m(dest,  {test: "destructured"}),
    m(destf, {test: "fully destructured"})

The snippet requires the latest version of Mithril.js framework. It is ideal for beginners showing some basic recipes.

In this example we can see an example of Mithril.js' m.mount API method, besides it core m() hyperscript function.

The code sample was authored by osban. It was last modified on 24 September 2020. Want to see more examples written by osban? Then Click here.


Do you see some improvements, that could be addressed here? Then let me know by opening an issue. As an alternative, you can fork the repository on GitHub, push your commits and send a pull request. To start your work, click on the edit link below. Thank you for contributing to this repo.

See more code examples  •  Edit this example on GitHub