-
Notifications
You must be signed in to change notification settings - Fork 0
/
index1.js
63 lines (57 loc) · 1.8 KB
/
index1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const redux = require('redux');
const createStore = redux.createStore
const combineReducer = redux.combineReducers
const BUY_CAKE= 'BUY_CAKE' ;
const BUY_ICECREAM = "BUY_ICECREAM"
//action :
function buyCake() {
return {
type : BUY_CAKE,
info : "Buying Cake"
}
}
function buyIceCream() {
return {
type : BUY_ICECREAM,
info : "Buying Ice Cream"
}
}
const initialState = {
noOfCakes : 10,
noOfIce : 20
}
//reducer
const CakeReducer =(state = initialState , action) =>{
switch(action.type){ // Here action type is checked and according to it the object is returned by reducer .
case BUY_CAKE : return{
...state,
noOfCakes : state.noOfCakes - 1
}
default : return state
}
}
const IceReducer =(state = initialState , action) =>{
switch(action.type){ // Here action type is checked and according to it the object is returned by reducer .
case BUY_ICECREAM : return{
...state,
noOfIce : state.noOfIce - 1
}
default : return state
}
}
// root reducer which combines the reducers
const rootReducer = combineReducer({
Cake : CakeReducer,
IceCream : IceReducer
})
const store = createStore(rootReducer)
console.log("Current State : " , store.getState());
const unsubscribe = store.subscribe(()=> console.log("Updated State : ", store.getState()))
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake()); //Buy Cake is action here. Dispatch tells store to buy cake .
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
unsubscribe()
// store.dispatch(buyIceCream()); after unsubscribing no instruction is carried by dispatch to store.