Skip to content

Instantly share code, notes, and snippets.

@fponticelli
Last active June 26, 2020 20:51
Show Gist options
  • Save fponticelli/9e611614663a28297e8440e641231c47 to your computer and use it in GitHub Desktop.
Save fponticelli/9e611614663a28297e8440e641231c47 to your computer and use it in GitHub Desktop.
import kempo.Reducer
import kempo.el
data class State(
val name: String,
val counter: Int = 0
) {
fun withName(name: String) = State(name, counter)
fun withCounter(counter: Int) = State(name, counter)
}
val template = el<State, State, Unit> {
className("my-class")
+"Hello "
el("button") {
onClick { state: State -> state.withCounter(state.counter + 1) }
text { it.name }
}
+" "
text { it.counter.toString() }
}
fun main() {
kempo.run(template, State(
name = "Franco Small"
))
}
import kempo.Reducer
import kempo.el
data class State(
val name: String,
val counter: Int = 0
) {
fun withName(name: String) = State(name, counter)
fun withCounter(counter: Int) = State(name, counter)
}
sealed class Action {
object Increment : Action()
}
val template = el<State, Action, Unit> {
className("my-class")
+"Hello "
el("button") {
onClick { -> Action.Increment }
text { it.name }
}
+" "
text { it.counter.toString() }
}
val reducer: Reducer<State, Action> = { state, action ->
when (action) {
is Action.Increment -> state.withCounter(state.counter + 1)
}
}
fun main() {
kempo.run(template, reducer, State(
name = "Franco Small"
))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment