forked from gcanti/elm-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.tsx
33 lines (27 loc) · 784 Bytes
/
Counter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as React from 'react'
import { cmd } from '../src'
import { Html } from '../src/React'
// --- Model
export type Model = number
export const init: [Model, cmd.Cmd<Msg>] = [0, cmd.none]
// --- Messages
export type Msg = { type: 'Increment' } | { type: 'Decrement' }
// --- Update
export function update(msg: Msg, model: Model): [Model, cmd.Cmd<Msg>] {
switch (msg.type) {
case 'Increment':
return [model + 1, cmd.none]
case 'Decrement':
return [model - 1, cmd.none]
}
}
// --- View
export function view(model: Model): Html<Msg> {
return dispatch => (
<div>
Count: {model}
<button onClick={() => dispatch({ type: 'Increment' })}>+</button>
<button onClick={() => dispatch({ type: 'Decrement' })}>-</button>
</div>
)
}