-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredux.js
More file actions
37 lines (30 loc) · 745 Bytes
/
redux.js
File metadata and controls
37 lines (30 loc) · 745 Bytes
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
// Reducer
import { legacy_createStore } from "redux";
const cartReducer = (
state = {
cart: [{ id: 1, qty: 1 }],
},
action
) => {
switch (action.type) {
case "ADD_TO_CART":
return {
...state,
cart: [...state.cart, action.payload],
};
default:
return state;
}
};
// Store = menyimpan state
const store = legacy_createStore(cartReducer);
console.log("oncreate store :", store.getState());
// Subscribe
store.subscribe(() => {
console.log("onchange store :", store.getState());
});
// Dispatch
const action1 = { type: "ADD_TO_CART", payload: { id: 2, qty: 1 } };
store.dispatch(action1);
const action2 = { type: "ADD_TO_CART", payload: { id: 3, qty: 4 } };
store.dispatch(action2);