flatstate
v0.1.1
Published
Prototypal inheritance applied to flat states.
Downloads
12
Maintainers
Readme
flatstate
A simple prototypal inheritance based state, inspired by this blog post.
The flatstate
function pollutes a generic class adding a setState
method.
Such method could also be borrowed at runtime.
class MyEl extends HTMLElement {
setState(state) {
flatstate.setState.call(this, state);
this.render(state);
}
render(state) {
this.textContent = state.name;
}
}
const el = new MyEl();
el.setState({name: 'test', clicks: 0});
el.addEventListener('click', () => {
this.setState({
clicks: this.state.clicks + 1
});
});
// or alternatively
const Class = flatstate(class {
// prototype will have a setState method
});