-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.combineReducers.html
98 lines (88 loc) · 2.54 KB
/
example.combineReducers.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!-- 测试 combineReducers -->
<!DOCTYPE html>
<html>
<head>
<title>Redux combineReducers example</title>
<script src="./index.js"></script>
</head>
<body>
<div>
<p>
Clicked counter1: <span id="value">0</span> times
<button id="increment">+</button>
<button id="decrement">-</button>
<br />
Clicked counter2: <span id="value2">1</span> times
<button id="MultipliedBy2">MultipliedBy2</button>
<button id="DividedBy2">DividedBy2</button>
</p>
</div>
<script>
function counter(state, action) {
if (typeof state === 'undefined') {
return 0
}
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
function counter2(state, action) {
if (typeof state === 'undefined') {
return 1
}
switch (action.type) {
case 'MultipliedBy2':
return state * 2
case 'DividedBy2':
return state / 2
default:
return state
}
}
const reducer = Redux.combineReducers({
counter,
counter2
});
console.info('combine reducer:', reducer) //*
var store = Redux.createStore(reducer) //*
var valueEl = document.getElementById('value')
var valueE2 = document.getElementById('value2')
function render() {
valueEl.innerHTML = store.getState().counter.toString()
valueE2.innerHTML = store.getState().counter2.toString()
console.info('store state:', store.getState()) //*
}
render()
store.subscribe(render)
document.getElementById('increment')
.addEventListener('click', function () {
store.dispatch({
type: 'INCREMENT'
})
})
document.getElementById('decrement')
.addEventListener('click', function () {
store.dispatch({
type: 'DECREMENT'
})
})
document.getElementById('MultipliedBy2')
.addEventListener('click', function () {
store.dispatch({
type: 'MultipliedBy2'
})
})
document.getElementById('DividedBy2')
.addEventListener('click', function () {
store.dispatch({
type: 'DividedBy2'
})
})
</script>
</body>
</html>