장바구니에 해당 상품이 있다면? count +1을 해주고
해당 상품이 없다면 상품을 장바구니에 추가하는 기능을 구현하고싶은데..
<<<<store.js>>
import { createSlice, configureStore } from "@reduxjs/toolkit";
import user from "./store/userSlice.js";
// state 변경함수들 남음
// {} 안에는 변경 함수명 넣기
let stock = createSlice({ name: "stock", initialState: [10, 11, 12] });
let cartproduct = createSlice({
name: "cartproduct",
initialState: [
{ id: 0, name: "White and Black", count: 2 },
{ id: 2, name: "Grey Yordan", count: 1 },
],
reducers: {
addCount(state, action) {
// array자료에서 내가 원하는 항목만 찾고싶다? state.findIndex
// findIndex는 array 뒤에만 붙일 수 있다.
let number = state.findIndex((a) => a.id === action.payload);
state[number].count++;
},
addItem(state, action) {
let number = state.findIndex((a) => a.id === action.payload);
console.log(number);
if (장바구니에 동일한 id가 있다면?) {
state[number].count++;
} else {
state.push(action.payload);
}
},
deleteItem(state, action) {
let item = state.filter((a) => a.id !== action.payload);
return item;
},
},
});
export let { addCount, addItem, deleteItem } = cartproduct.actions;
export default configureStore({
reducer: {
user: user.reducer,
stock: stock.reducer,
cartproduct: cartproduct.reducer,
},
});
해당 부분을 구현을 못하겠습니다.. 도와주세요,,, 혹시 추가로 생각해야할 부분이 있나요?