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.

google clone Part17 Pagination実装

Last updated at Posted at 2023-05-19

概要

目次

検索結果の2ページ目を表示できるよう実装します。
以下gifアニメは実装後の様子です。

nnnmn..,.m,.m,.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"

実装のポイント

APIのパラメーターstartを追加します。
1ページあたり10件表示するようにします。

Start=1 最初の1ページ目

Nextを押したときの処理

image.png

Start=11 2ページ目の場合

2ページからは前のページに進めるリンクを表示します。

Previousを押したときの処理

image.png

justify-betweenでPreviousとNextを配置

image.png

コード部分

PaginationButtons

PaginationButtons.jsx
"use client"

import Link from "next/link";
import {usePathname,useSearchParams} from "next/navigation";
import { BsChevronLeft,BsChevronRight } from "react-icons/bs";


export default function PaginationButton(){
    const pathname = usePathname();
    const searchParams = useSearchParams();
    const searchTerm = searchParams.get("searchTerm");
    const startIndex = +searchParams.get("start") || 1;

    return(
        <div className="text-blue-700 flex px-10 pb-4 justify-between sm:justify-start sm:space-x-44 sm:px-0">
      {startIndex >= 10 && (
        <Link   href={`${pathname}?searchTerm=${searchTerm}&start=${startIndex - 10}`}>
            <div className="flex flex-col cursor-pointer items-center hover:underline">
                <BsChevronLeft clssName="h-5" />
                <p>Previous</p>
            </div>
        </Link>
      )}
      {startIndex <= 90 && (
        <Link
          href={`${pathname}?searchTerm=${searchTerm}&start=${startIndex + 10}`}
        >
          <div className="flex flex-col cursor-pointer items-center hover:underline">
            <BsChevronRight className="h-5" />
            <p>Next</p>
          </div>
        </Link>
      )}
        </div>
    )
}

WebSearchResults

WebSearchResults.jsx
import Link from "next/link";
import Parser from "html-react-parser";
import PaginationButtons from "./PaginationButtons";

export default function WebSearchResults({results}){
   return(
    <div className="w-full mx-auto px-3 pb-24 sm:pl-[5%] md:pl-[14%] lg:pl-52">
        <p className="text-gray-600 text-sm mb-5 mt-3">
            About {results.searchInformation?.formattedTotalResults}
            results ({results.searchInformation?.formattedSearchTime} seconds)
        </p>  
        {results.items?.map((result) => (
            <div className="mb-8 max-w-xl" key={result.link}>
                <div className="group flex flex-col">
                    <Link className="text-sm truncate" href={result.link}>
                        {result.formattedUrl}
                    </Link>
                    <Link className="group-hover:underline decoration-blue-800 text-xl truncate font-medium text-blue-900" href={result.link}>
                        {result.title}
                    </Link>
                    <p className="text-gray-600">
                        {Parser(result.htmlSnippet)}
                    </p>
                </div>
            </div>
        ))}
        <div className="ml-16">
+            <PaginationButtons />
        </div>
    </div>
   ) 
}

参考

css

その他

image.png

Next.js usePathname()

image.png

Next.js useSearchParams()

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?