Redux의 state객체 업데이트는 immutable하게 변경해야 한다.
원본 객체가 아니라 복사본에서 업데이트를 하도록 만든다.
불변 업데이트란? => 얕은 복사본을 만드는 것 (깊은 복사 아님!!)
function todoApp(state = initialState, action) {
const filtered = state.cartItems.filter(
(el) => el.itemId !== action.payload.itemId
);
switch (action.type) {
case REMOVE_FROM_CART:
return { ...state, cartItems: filtered, }
default:
return state
}
}
function todoApp(state = initialState, action) {
const filtered = state.cartItems.filter(
(el) => el.itemId !== action.payload.itemId
);
switch (action.type) {
case REMOVE_FROM_CART:
return Object.assign({}, state, {
cartItems: filtered,
});
default:
return state
}
}
https://redux.js.org/usage/structuring-reducers/immutable-update-patterns