-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8redux-counter1.html
62 lines (58 loc) · 1.53 KB
/
8redux-counter1.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
<!DOCTYPE html>
<html>
<head>
<script src="https://fb.me/react-0.14.0.js"></script>
<script src="https://fb.me/react-dom-0.14.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">
const counter = (state = 0, action) => {
console.log(action.type);
switch (action.type){
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
const Counter = ({
value,
onIncrement,
onDecrement
}) => (
<div>
<h1>{value}</h1>
<button onClick={onIncrement}> + </button>
<button onClick={onDecrement}> - </button>
</div>
);
const { createStore } = Redux;
const store = createStore(counter);
const render = () =>{
ReactDOM.render(
<Counter
value={ store.getState() }
onIncrement={() =>
store.dispatch({
type: 'INCREMENT'
})
}
onDecrement={() =>
store.dispatch({
type: 'DECREMENT'
})
}
/>,
document.getElementById('example')
);
};
store.subscribe(render);
render();
</script>
</body>
</html>