Skip to content

Commit 644b899

Browse files
committed
refactor(Sprint9): SSR 방식으로 초기 화면 로딩
1 parent 59e35c3 commit 644b899

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

my-app/pages/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ import TodoList from "@/components/todo/todo-list";
88
import { useAsyncCall } from "@/hooks/use-async-call";
99
import { addTodo, getTodos, toggleTodo } from "@/libs/apis/todo";
1010
import styles from "@/styles/home.module.css";
11-
import { useEffect, useMemo, useState } from "react";
11+
import { useMemo, useState } from "react";
1212

13-
export default function Home() {
13+
export async function getServerSideProps() {
14+
const todos = await getTodos();
15+
return { props: { todos } };
16+
}
17+
18+
export default function Home({ todos: initialTodos }) {
1419
const [inputValue, setInputValue] = useState("");
15-
const [todos, setTodos] = useState([]);
20+
const [todos, setTodos] = useState(initialTodos ?? []);
1621
const [isLoading, execute] = useAsyncCall();
1722

1823
const canAdd = useMemo(() => inputValue.trim().length > 0, [inputValue]);
@@ -51,13 +56,6 @@ export default function Home() {
5156
});
5257
};
5358

54-
useEffect(() => {
55-
execute(async () => {
56-
const fetchedTodos = await getTodos();
57-
setTodos(fetchedTodos);
58-
});
59-
}, []);
60-
6159
return (
6260
<>
6361
<GlobalNavBar />

0 commit comments

Comments
 (0)