listPosts API를 호출할 때 파라미터로 값을 넣어 주면 /api/posts?username=tester&page=2와 같이 주소를 만들어서 호출합니다.

    이제 위 요청의 상태를 관리하는 리덕스 모듈을 만들어 봅시다. modules 디렉터리에 posts.js 파일을 만들어서 다음 코드를 작성하세요.

    modules/posts.js

    import { createAction, handleActions } from 'redux-actions';
    import createRequestSaga, {
      createRequestActionTypes,
    } from '../lib/createRequestSaga';
    import * as postsAPI from '../lib/api/posts';
    import { takeLatest } from 'redux-saga/effects';
    
    const [
      LIST_POSTS,
      LIST_POSTS_SUCCESS,
      LIST_POSTS_FAILURE,
    ] = createRequestActionTypes('posts/LIST_POSTS');
    
    export const listPosts = createAction(
      LIST_POSTS,
      ({ tag, username, page }) => ({ tag, username, page }),
    );
    
    const listPostsSaga = createRequestSaga(LIST_POSTS, postsAPI.listPosts);
    export function* postsSaga() {
      yield takeLatest(LIST_POSTS, listPostsSaga);
    }
    
    const initialState = {
      posts: null,
      error: null,
    };
    
    const posts = handleActions(
      {
        [LIST_POSTS_SUCCESS]: (state, { payload: posts }) => ({
          ...state,
          posts,
        }),
        [LIST_POSTS_FAILURE]: (state, { payload: error }) => ({
          ...state,
          error,
        }),
      },
      initialState,
    );
    
    export default posts;

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