Level: beginner • Mithril.js Version: latest
component counter m.mount pojo state
This is another example showing a very simple JavaScript closure. It is one of the classic examples, that shows a counter, that can be increased or decreased by clicking one of two appropriate buttons. Handling state with JavaScript closures is one of the recommended ways in Mithril.js.
Type | Name | URL |
---|---|---|
script | mithril@latest | https://unpkg.com/mithril@latest |
const closure = () => {
let count = 0
return {
view: () =>
m('div',
m('h1', `Count: ${count}`),
m('button', {onclick: () => count++}, "+"),
m('button', {onclick: () => count--}, "-")
)
}
}
m.mount(document.body, closure)
As a prerequisite for this snippet, the latest version of Mithril.js framework is required. Beginners should have no problems following this example, that simply shows some basic recipies.
In this code sample Mithril.js' m.mount
API method is use, besides the basic hyperscript function m().
The example was written by osban, last edits were made on 24 September 2020. The author has contributed some more snippets. Click here to see them all.
Did you note a typo or something else? So let me know by opening an issue. Or much better: just fork the repository on GitHub, push your commits and send a pull request. Ready to start your work? Then click on the edit link below. Thanks in advance!