Skip to content

Commit

Permalink
refactor: render PostListPage to ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
syoung125 committed May 29, 2024
1 parent 313dc09 commit f29e0bf
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions pages/posts.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState, useEffect } from "react";

import { useRouter } from "next/router";
import styled from "styled-components";
import { GetServerSidePropsContext } from "next";

import ScrollPagenation from "@src/components/common/scroll-pagenation";
import PostListItem from "@src/components/post-list/post-list-item";
Expand All @@ -10,19 +9,14 @@ import { breakpoints } from "@src/styles/theme";
import PostService from "@src/services/post.service";
import { Post } from "@src/types/post.type";

export default function PostListPage() {
type PostListPageProps = {
posts: Post[];
};

export default function PostListPage({ posts }: PostListPageProps) {
const router = useRouter();
const tag = String(router.query.tag);

const [posts, setPosts] = useState<Post[]>([]);

useEffect(() => {
(async () => {
const posts: Post[] = await PostService.getPosts({ tag });
setPosts(posts);
})();
}, [tag]);

return (
<Container>
<Wrapper>
Expand All @@ -35,6 +29,13 @@ export default function PostListPage() {
);
}

export async function getServerSideProps(context: GetServerSidePropsContext) {
const tag = String(context.query.tag);
const posts: Post[] = await PostService.getPosts({ tag });

return { props: { posts } };
}

const Container = styled.div`
flex: 1;
Expand Down

0 comments on commit f29e0bf

Please sign in to comment.