이번에는 리듀서 함수도 더 간단하고 가독성 높게 작성해 보겠습니다. handleActions라는 함수를 사용합니다.

    modules/counter.js

    import { createAction, handleActions } from 'redux-actions';
    
    const INCREASE = 'counter/INCREASE';
    const DECREASE = 'counter/DECREASE';
    
    export const increase = createAction(INCREASE);
    export const decrease = createAction(DECREASE);
    
    const initialState = {
      number: 0,
    };
    
    const counter = handleActions(
      {
        [INCREASE]: (state, action) => ({ number: state.number + 1 }),
        [DECREASE]: (state, action) => ({ number: state.number - 1 }),
      },
      initialState,
    );
    
    export default counter;

     

    handleActions 함수의 첫 번째 파라미터에는 각 액션에 대한 업데이트 함수를 넣어 주고, 두 번째 파라미터에는 초기 상태를 넣어 줍니다.

    어떤가요? 코드가 훨씬 짧아지고 가독성이 높아졌죠?

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.