-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux-demo.js
36 lines (30 loc) · 927 Bytes
/
redux-demo.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
const redux = require('redux');
const initialState = {
totalLikesCount: 0,
listCount: 0,
blogItem: 0,
}
//Reducer
const mainReducer = (state = initialState, action) => {
if(action.type === 'LIKE_CLICKED') {
return {...state, totalLikesCount: state.totalLikesCount + 1}
}
if(action.type === 'DISLIKE_CLICKED') {
return {...state, totalLikesCount: state.totalLikesCount - 1}
}
if(action.type === 'LIKETIMES') {
return {...state, totalLikesCount: state.totalLikesCount + action.likeCount}
}
return state
}
//State
const globalState = redux.createStore(mainReducer);
// console.log(globalState.getState());
//Subscription
globalState.subscribe(() => {
console.log(globalState.getState());
});
//Dispatch
globalState.dispatch({type: 'LIKE_CLICKED'});
globalState.dispatch({type: 'DISLIKE_CLICKED'});
globalState.dispatch({type: 'LIKETIMES', likeCount: 100});