Parent-Child Communication

by osban

Level: beginner • Mithril.js Version: latest

This example demonstrates parent-child communication where a child changes the parent. This is one of the basic concepts in Mithril.js, of how to communicate between child and parent components.

Live Example

Dependencies

Type Name URL
scriptmithril@latesthttps://unpkg.com/mithril@latest

JavaScript

const getmsg = {
  view: v =>
    m("input", {
      value: v.attrs.msg,
      oninput: e => v.attrs.change(e.target.value)
    })
}

const app = () => {
  let msg = 'start'

  return {
    view: () => [
      m(getmsg, {
        msg,
        change: value => {msg = value}
      }),
      m('h1', msg)
    ]
  }
}

m.mount(document.body, app)

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.

Contribute

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