Level: intermediate • Mithril.js Version: latest
component m.mount mitosis state
An example managing state using Factory Functions with Stateless Components. It was made by the help of porsager and foxdonut.
Type | Name | URL |
---|---|---|
script | mithril@latest | https://unpkg.com/mithril@latest |
const State = () => ({ count: 0 });
const Actions = state => ({
increment: () => state.count += 1,
decrement: () => state.count -= 1
});
const Counter = (state, actions) =>
m('div',
m('h1', 'Counter'),
m('p', state.count),
m('button', { onclick: actions.increment }, '+'),
m('button', { onclick: actions.decrement }, '-'),
Child(state, actions)
)
;
const Child = (state, actions) =>
m('div',
m('h2', 'Child'),
m('p', state.count * 2),
m('button', { onclick: actions.increment }, '+'),
m('button', { onclick: actions.decrement }, '-'),
)
;
m.mount(document.body, () => {
const state = State();
const actions = Actions(state);
return { view: () => Counter(state, actions) };
});
As a prerequisite for this snippet, the latest version of Mithril.js framework is required. The example targets intermediate users, that are familiar with with most aspects of the framework.
In this code sample Mithril.js' m.mount
API method is use, besides the basic hyperscript function m().
The example was written by kevinfiol, last edits were made on 16 October 2021. 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!