Last active
August 17, 2017 10:28
-
-
Save vdsabev/b97e25248608bdc1158f41bd33105776 to your computer and use it in GitHub Desktop.
Exploring Unidirectional Components in Mithril
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const component = (options = {}) => (vnode) { | |
let state = { ...options.state, ...vnode.attrs }; | |
const proxyActions = {}; | |
Object.keys(options.actions).forEach((key) => { | |
proxyActions[key] = (...args) => { | |
const newState = options.actions[key](state, proxyActions, ...args); | |
if (newState) { | |
state = { ...state, ...newState }; | |
} | |
return state; | |
}; | |
}); | |
return { | |
...options.events, | |
view: () => options.view(vnode, state, proxyActions) | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment