-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_adding_react.js
46 lines (40 loc) · 970 Bytes
/
03_adding_react.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
// http://jsbin.com/koyoduhito/11/edit?js,output
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
const { createStore } = Redux;
const store = createStore(counter);
const Counter = ({ value, onIncrement, onDecrement }) => {
return <div>
<h1> {value} </h1>
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
</div>
};
const render = () => {
ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() => {
store.dispatch({
type: 'INCREMENT'
})
}}
onDecrement={() => {
store.dispatch({
type: 'DECREMENT'
})
}}
/>,
document.getElementById('root')
);
}
store.subscribe(render);
render();