이제 기존에 구현했던 사가를 다음과 같이 짧은 코드로 구현할 수 있습니다.

    modules/sample.js

    import { createAction, handleActions } from 'redux-actions';
    import { takeLatest } from 'redux-saga/effects';
    import * as api from '../lib/api';
    import createRequestSaga from '../lib/createRequestSaga';
    
    // 액션 타입을 선언합니다.
    const GET_POST = 'sample/GET_POST';
    const GET_POST_SUCCESS = 'sample/GET_POST_SUCCESS';
    
    const GET_USERS = 'sample/GET_USERS';
    const GET_USERS_SUCCESS = 'sample/GET_USERS_SUCCESS';
    
    export const getPost = createAction(GET_POST, id => id);
    export const getUsers = createAction(GET_USERS);
    
    const getPostSaga = createRequestSaga(GET_POST, api.getPost);
    const getUsersSaga = createRequestSaga(GET_USERS, api.getUsers);
    
    
    export function* sampleSaga() {
      yield takeLatest(GET_POST, getPostSaga);
      yield takeLatest(GET_USERS, getUsersSaga);
    }
    
    // 초기 상태를 선언합니다.
    // 요청의 로딩 중 상태는 loading이라는 객체에서 관리합니다.
    
    const initialState = {
      post: null,
      users: null
    };
    
    const sample = handleActions(
      {
        [GET_POST_SUCCESS]: (state, action) => ({
          ...state,
          post: action.payload
        }),
        [GET_USERS_SUCCESS]: (state, action) => ({
          ...state,
          users: action.payload
        })
      },
      initialState
    );
    
    export default sample;

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