Files
leporello-js/docs/examples/preact/stateful.js
Dmitry Vasilev c3365fe1ee async calls WIP
2022-10-26 13:11:51 +08:00

32 lines
657 B
JavaScript

import {Component} from 'https://unpkg.com/preact?module';
export const Stateful = ({getInitialState, handlers, render}) => {
return class extends Component {
constructor() {
super()
this.compState = getInitialState()
this.handlers = Object.fromEntries(
Object
.entries(handlers)
.map(([name, h]) =>
[name, this.makeHandler(h)]
)
)
}
makeHandler(h) {
return (...args) => {
this.compState = h(this.compState, ...args)
this.forceUpdate()
}
}
render() {
return render(this.props, this.compState, this.handlers)
}
}
}