mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-14 05:14:28 -08:00
32 lines
657 B
JavaScript
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)
|
|
}
|
|
}
|
|
|
|
}
|