-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter-reducer.js
59 lines (49 loc) · 1.17 KB
/
counter-reducer.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const initState = {
value: 0,
loading: false,
}
function counterReducer(state = initState, action) {
switch (action.type) {
case 'INCREMENT':
return {
...state,
value: state.value + 1,
}
case 'DECREMENT':
return {
...state,
value: state.value - 1,
}
default:
return state
}
}
const reducers = combineReducers({
todo: counterReducer,
})
const store = createStore(
reducers,
applyMiddleware(logger, window.ReduxThunk.default)
)
const $ = document.getElementById.bind(document)
const counter = $('counter')
const inc = $('inc')
const dec = $('dec')
const incIn1 = $('incIn1')
store.subscribe((state) => {
const { value } = state.todo
counter.textContent = value
})
inc.addEventListener('click', () => {
store.dispatch({ type: 'INCREMENT' })
})
dec.addEventListener('click', () => {
store.dispatch({ type: 'DECREMENT' })
})
incIn1.addEventListener('click', () => {
store.dispatch((dispatch) => {
setTimeout(() => {
dispatch({ type: 'INCREMENT' })
}, 1000)
})
})