-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (54 loc) · 1.33 KB
/
index.js
File metadata and controls
64 lines (54 loc) · 1.33 KB
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
import { configureStore } from '@reduxjs/toolkit';
import { bindActionCreators } from '@reduxjs/toolkit';
// const createstore = createStore;
const CAKE_ORDERED = "CAKE_ORDERED";
const CAKE_RESTOCKED = "CAKE_RESTOCKED";
const OrderCake = () => {
return {
type: CAKE_ORDERED,
payload: 1,
};
};
const RestockCake = (qty) => {
return {
type: CAKE_RESTOCKED,
payload: qty,
};
};
const initialState = {
cakeCount: 10,
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case CAKE_ORDERED:
return {
...state,
cakeCount: state.cakeCount - action.payload,
};
case CAKE_RESTOCKED:
return {
...state,
cakeCount: state.cakeCount + action.payload,
};
default:
return state;
}
};
const store = configureStore({ reducer });
console.log("Initial state", store.getState());
const unsubscribe = store.subscribe(() => {
console.log("Updated state", store.getState());
});
// store.dispatch(OrderCake({
// type: CAKE_ORDERED,
// quantity: 1,
// }));
// store.dispatch(OrderCake());
// store.dispatch(OrderCake());
// store.dispatch(RestockCake(5));
const actions = bindActionCreators({ OrderCake, RestockCake }, store.dispatch);
actions.OrderCake();
actions.OrderCake();
actions.OrderCake();
actions.RestockCake(5);
unsubscribe();