이 리듀서를 만든 다음에는 루트 리듀서에도 등록해 주세요.

    modules/index.js

    import { combineReducers } from 'redux';
    import auth from './auth';
    import loading from './loading';
    
    const rootReducer = combineReducers({
      auth,
      loading,
    });
    
    export default rootReducer;

     

    이어서 lib 디렉터리에 새 파일을 만들고 createRequestSaga 함수를 작성하세요.

    lib/createRequestSaga.js

    import { call, put } from 'redux-saga/effects';
    import { startLoading, finishLoading } from '../modules/loading';
    
    export default function createRequestSaga(type, request) {
      const SUCCESS = `${type}_SUCCESS`;
      const FAILURE = `${type}_FAILURE`;
    
      return function*(action) {
        yield put(startLoading(type)); // 로딩 시작
        try {
          const response = yield call(request, action.payload);
          yield put({
            type: SUCCESS,
            payload: response.data,
          });
        } catch (e) {
          yield put({
            type: FAILURE,
            payload: e,
            error: true,
          });
        }
        yield put(finishLoading(type)); // 로딩 끝
      };
    }

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