— javascript, immer — 1 min read
아래는 redux에서 reducer쪽 immutable state를 immer로 handling하는 예제이다.
1import { createAction, handleActions } from "redux-actions";2import produce from "immer";34// Action Types5export const INCREMENT = "INCREMENT";6export const increment = createAction(INCREMENT);78const defaultState = {9 count: 0,10};
1const reducer = handleActions(2 {3 [INCREMENT]: (baseState) =>4 produce(baseState, ({ count }) => {5 count = count + 1;6 }),7 },8 defaultState9);1011export default reducer;
1const reducer = handleActions(2 {3 [INCREMENT]: (baseState) =>4 produce(baseState, (draft) => {5 draft.count = draft.count + 1;6 }),7 },8 defaultState9);1011export default reducer;