forked from purescript-react/purescript-react-basic-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReducer.purs
35 lines (29 loc) · 895 Bytes
/
Reducer.purs
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
34
35
module Reducer where
import Prelude
import React.Basic.DOM as R
import React.Basic.Events (handler_)
import React.Basic.Hooks (CreateComponent, component, fragment, useReducer, (/\))
import React.Basic.Hooks as React
data Action
= Increment
| Decrement
mkReducer :: CreateComponent {}
mkReducer = do
component "Reducer" \props -> React.do
state /\ dispatch <-
useReducer { counter: 0 } \state -> case _ of
Increment -> state { counter = state.counter + 1 }
Decrement -> state { counter = state.counter - 1 }
pure $ fragment
[ R.button
{ onClick: handler_ $ dispatch Decrement
, children: [ R.text $ "Decrement" ]
}
, R.button
{ onClick: handler_ $ dispatch Increment
, children: [ R.text $ "Increment" ]
}
, R.div_
[ R.text $ show state.counter
]
]