mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 13:04:30 -08:00
28 lines
618 B
JavaScript
28 lines
618 B
JavaScript
import { Component } from "preact"
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|