Skip to content

Commit 9ce6743

Browse files
2.3 Server Side Rendering
1 parent ebe7016 commit 9ce6743

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

pages/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import { useEffect, useState } from "react";
22
import Seo from "../components/Seo";
33

4-
export default function Home() {
5-
const [movies, setMovies] = useState();
6-
useEffect(() => {
7-
(async () => {
8-
const { results } = await (await fetch(`/api/movies`)).json();
9-
setMovies(results);
10-
})();
11-
}, []);
4+
export default function Home({ results }) {
125
return (
136
<div className="container">
147
<Seo title="Home" />
15-
{!movies && <h4>Loading...</h4>}
16-
{movies?.map((movie) => (
8+
{results?.map((movie) => (
179
<div className="movie" key={movie.id}>
1810
<img src={`https://image.tmdb.org/t/p/w500/${movie.poster_path}`} />
1911
<h4>{movie.original_title}</h4>
@@ -43,3 +35,14 @@ export default function Home() {
4335
</div>
4436
);
4537
}
38+
39+
export async function getServerSideProps() {
40+
const { results } = await (
41+
await fetch(`http://localhost:3000/api/movies`)
42+
).json();
43+
return {
44+
props: {
45+
results,
46+
},
47+
};
48+
}

0 commit comments

Comments
 (0)