0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

imdb clone Part20(完) 13 search ability 2/2 検索結果のページを実装

Posted at

概要

今回は検索後表示されるページを実装します。
下gifアニメは実装後の様子です。

pppppppppppppnnnnn.gif

開発環境

OS:Windows10
IDE:VSCode

"@next/font": "13.1.5",
"autoprefixer": "10.4.14",
"eslint": "8.39.0",
"eslint-config-next": "13.3.1",
"next": "13.3.1",
"postcss": "8.4.23",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-icons": "^4.8.0",
"tailwindcss": "3.3.2"

実装のポイント

app/search/[searchTerm]/page.jsxを新規作成して検索結果のページを作ります。
pageが開かれる段階でTMDBのAPIを使い該当する映画を取得します。

image.png

コード部分

app/search/[searchTerm]/page.jsx

page.jsx
import Results from "@/components/Results";

export default async function SearchPage({params}){
    const res = await fetch(`https://api.themoviedb.org/3/search/movie?api_key=${process.env.API_KEY}&query=${params.searchTerm}&language=en-US&include_adult=false`)
    if(!res.ok){
        throw new Error("Error fetching data")
    }

    const data = await res.json();

    const results = data.results

    return(
        <div>
            {results && results.length === 0 && (
                <h1 className="text-center pt-6">No results found</h1>
            )}
            {results && <Results results={results}/>}
        </div>
    )
}

参考

TMDB API Search

image.png

その他

Udemy

githubコミット分

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?