-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsx-redux.html
79 lines (73 loc) · 1.91 KB
/
jsx-redux.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://fb.me/react-with-addons-15.0.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.min.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.34/browser.min.js"></script>
<title>React Template with CDN Resources - JSX, redux</title>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
console.log('start');
let Component = React.Component;
const counter = (state = 0, action) => {
console.log('reducer state:', state);
console.log('reducer action:', action);
switch(action.type){
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
//created a basic counter using redux
const { createStore } = Redux;
// same as
// var createStore = Redux.createStore; //es5
// import { createStore } from 'redux' //babel import
const store = createStore(counter);
// created a new reducer using pure function counter
// creating a react dom component Counter <Counter />
const Counter = ({
value,
onIncrement,
onDecrement
}) => (
<div>
<h1>{value}</h1>
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
</div>
);
class App extends Component {
render() {
return (
<Counter value={store.getState()}
onIncrement={() =>
store.dispatch({
type : 'INCREMENT'
})
}
onDecrement={() =>
store.dispatch({
type : 'DECREMENT'
})
}
/>
);
}
}
const render = () => {
ReactDOM.render(<App/>,document.getElementById('app'));
};
store.subscribe(render);
render();
console.log('done');
</script>
</body>
</html>