-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcartSlice.js
33 lines (29 loc) · 982 Bytes
/
cartSlice.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
import { createSlice } from "@reduxjs/toolkit"
const cartSlice = createSlice({
name: "cart",
initialState: {
items: []
},
reducers: {
// "addItem":" is action & "(state action)=>{}" is reducer function
addItem: (state, action) => {
state.items.push(action.payload)
// return /* Note: Never return from reducer function */
},
removeItem: (state, action) => {
// removing the last item from the array
state.items.pop()
/* Find the element in the array & remove it */
// console.log(action.payload)
// const elementToRemove = action.payload
// const indexToRemove = state.items.indexOf(elementToRemove)
// console.log("elementToRemove: ", elementToRemove)
// console.log("indexToRemove: ", indexToRemove)
},
clearCart: state => {
state.items = []
}
}
})
export const { addItem, removeItem, clearCart } = cartSlice.actions
export default cartSlice.reducer