-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.html
44 lines (40 loc) · 1.42 KB
/
index.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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Learn Redux</title>
<link rel="shortcut icon" type="image/png" href="http://www.favicon.cc/logo3d/805435.png"/>
</head>
<body style='margin: 0 1em;'>
<div id='main'>
<h1>Learn <em>Redux</em></h1>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.5/redux.min.js"></script>
<script>
// counter is the reducer that manages state updates:
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
const { createStore } = Redux; // Redux is GLOBAL Object from redux.min.js
// create the store for our mini-app using the counter reducer
const store = createStore(counter);
console.log(store.getState()); // counter should be 0 (zero)
store.dispatch({type:'INCREMENT'}); // increment from zero to one
console.log(store.getState()); // counter is 1 (one)
const render = () => {
document.body.innerText = store.getState();
}
store.subscribe(render);
render(); // render the initial state of the page/app
document.addEventListener('click', () => {
store.dispatch({type:'INCREMENT'});
});
</script>
</body>
</html>