Level: beginner • Mithril.js Version: latest
Re-init a component every time.
Type | Name | URL |
---|---|---|
script | mithril@latest | https://unpkg.com/mithril@latest |
const closure = () => {
console.log('init closure')
return {
view: () => [
m('button', {onclick: () => m.route.set('/plop/123')}, 'click'),
m('div', 'test')
]
}
}
const pojo = {
oninit: () => console.log('init pojo'),
view: () => [
m('button', {onclick: () => m.route.set('/plop/123')}, 'click'),
m('div', 'test')
]
}
m.route(document.body, '/plop/123', {
'/plop/:id': {
onmatch: () => ({view: v => m(closure, v.attrs)})
// onmatch: () => ({...pojo})
}
})
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.route
API method, besides it core m() hyperscript function.
It is also showing the oninit
hook, which is one of several Mithril.js' lifecycle methods.
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.