다음으로 containers 디렉터리 안에 posts 디렉터리를 만들고, 그 안에 PostListContainer 컴포넌트를 만듭니다. 이 컴포넌트는 주소에 있는 쿼리 파라미터를 추출하여 우리가 만들었던 listPosts API를 호출해 줍니다.

    containers/posts/PostListContainer.js

    import React, { useEffect } from 'react';
    import qs from 'qs';
    import { withRouter } from 'react-router-dom';
    import { useDispatch, useSelector } from 'react-redux';
    import PostList from '../../components/posts/PostList';
    import { listPosts } from '../../modules/posts';
    
    const PostListContainer = ({ location }) => {
      const dispatch = useDispatch();
      const { posts, error, loading, user } = useSelector(
        ({ posts, loading, user }) => ({
          posts: posts.posts,
          error: posts.error,
          loading: loading['posts/LIST_POSTS'],
          user: user.user,
        }),
      );
      useEffect(() => {
        const { tag, username, page } = qs.parse(location.search, {
          ignoreQueryPrefix: true,
        });
        dispatch(listPosts({ tag, username, page }));
      }, [dispatch, location.search]);
    
      return (
        <PostList
          loading={loading}
          error={error}
          posts={posts}
          showWriteButton={user}
        />
      );
    };
    
    export default withRouter(PostListContainer);

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