선생님. 안녕하세요!
장바구니에 없으면 추가,
있으면 카운트를 올리는 응용문제를 해결해보려고 하는데요
reducers 안에 함수들끼리 호출할 때는 뭔가 다른 방법이 있는 걸까요?
어디서 보고 함수 호출시 앞에 this. 붙여봤지만 오히려
Uncaught TypeError: Cannot read properties of undefined (reading 'countUp')
라는 문구만 나와서요...
어떻게 해야하는 건지 찾아봐도 도저히 모르겠어서 질문 드립니다.
reducers: {
countUp(state, action) {
console.log(action.payload)
let findId = state.findIndex((e)=>{
return e.id===action.payload
})
state[findId].count++
},
addItem(state, action){
// action.payload에 id가 기존 어레이에 존재하는지 검사
// 값이 존재하지 않으면 배열에 추가
//그렇지 않으면 countUp()
var findItem = state.find(e=>e.id===action.payload.id)
if(findItem==null){
console.log(findItem, '장바구니에 없음. 추가!')
state.push(action.payload)
}
else{
console.log('장바구니에 있었음. 수량 업')
console.log(action.payload.id)
countUp(action.payload.id)
}
}