Claude Code के साथ Implement Pagination कैसे करें
Claude Code का उपयोग करके implement pagination सीखें। Practical code examples और step-by-step guidance शामिल है।
pageネーションimplementationको Claude Code से Efficient बनाएं
pageネーションは一見シンプルですが、UIcomponent、API設計、queryoptimizationと幅広い知識が求められ है।Claude Code से、要件を伝える से हीフロントエンド सेバックエンド तक一貫したimplementation generateでき है।
オフセットベースのpageネーション
もっともbasic pattern है।Claude Codeにअगला की तरह依頼し है।
> pageネーション付きの記事listcomponentをReactで作って。
> 1page10件、page番号display、पहलेबाद移動buttonあり。
interface PaginationProps {
currentPage: number;
totalPages: number;
onPageChange: (page: number) => void;
}
export function Pagination({ currentPage, totalPages, onPageChange }: PaginationProps) {
const pages = Array.from({ length: totalPages }, (_, i) => i + 1);
// displayするpage番号を絞る(現在pageのपहलेबाद2page)
const visiblePages = pages.filter(
(p) => p === 1 || p === totalPages || Math.abs(p - currentPage) <= 2
);
return (
<nav className="flex items-center gap-1">
<button
onClick={() => onPageChange(currentPage - 1)}
disabled={currentPage === 1}
className="px-3 py-1 rounded disabled:opacity-50"
>
Previous
</button>
{visiblePages.map((page, i) => {
// 省略記号のdisplay
const prev = visiblePages[i - 1];
const showEllipsis = prev && page - prev > 1;
return (
<span key={page}>
{showEllipsis && <span className="px-2">...</span>}
<button
onClick={() => onPageChange(page)}
className={`px-3 py-1 rounded ${
page === currentPage ? "bg-blue-600 text-white" : "hover:bg-gray-100"
}`}
>
{page}
</button>
</span>
);
})}
<button
onClick={() => onPageChange(currentPage + 1)}
disabled={currentPage === totalPages}
className="px-3 py-1 rounded disabled:opacity-50"
>
Next
</button>
</nav>
);
}
カーソルベースのpageネーション
大量data के case मेंはカーソルベースが効率的 है।APIとフロントエンドの両方を一度にgenerateでき है।
// APIroute(Next.js App Router)
import { db } from "@/lib/database";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const cursor = searchParams.get("cursor");
const limit = parseInt(searchParams.get("limit") || "20");
const items = await db.post.findMany({
take: limit + 1, // अगलाpageの存在confirm用に1件多くfetch
...(cursor && {
cursor: { id: cursor },
skip: 1,
}),
orderBy: { createdAt: "desc" },
});
const hasMore = items.length > limit;
const data = hasMore ? items.slice(0, -1) : items;
const nextCursor = hasMore ? data[data.length - 1].id : null;
return Response.json({ data, nextCursor, hasMore });
}
無限scrollのimplementation
Intersection Observer APIを使った無限scrollも、Claude Code से簡潔にimplementationでき है।
import { useEffect, useRef, useState, useCallback } from "react";
interface Item {
id: string;
title: string;
}
export function InfiniteList() {
const [items, setItems] = useState<Item[]>([]);
const [cursor, setCursor] = useState<string | null>(null);
const [hasMore, setHasMore] = useState(true);
const [loading, setLoading] = useState(false);
const loaderRef = useRef<HTMLDivElement>(null);
const loadMore = useCallback(async () => {
if (loading || !hasMore) return;
setLoading(true);
const params = new URLSearchParams({ limit: "20" });
if (cursor) params.set("cursor", cursor);
const res = await fetch(`/api/items?${params}`);
const data = await res.json();
setItems((prev) => [...prev, ...data.data]);
setCursor(data.nextCursor);
setHasMore(data.hasMore);
setLoading(false);
}, [cursor, hasMore, loading]);
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) loadMore();
},
{ threshold: 0.1 }
);
if (loaderRef.current) observer.observe(loaderRef.current);
return () => observer.disconnect();
}, [loadMore]);
return (
<div>
{items.map((item) => (
<div key={item.id} className="p-4 border-b">
{item.title}
</div>
))}
<div ref={loaderRef} className="p-4 text-center">
{loading && "Loading..."}
{!hasMore && "All items displayed"}
</div>
</div>
);
}
basic use करने का तरीकाはClaude Codeのintroduction guideを、プロンプトの工夫はプロンプトを改善する5つのTipsदेखें。
Summary
Claude Code का उपयोग करके、オフセット型、カーソル型、無限scroll आदि、要件に合ったpageネーションをकम समय मेंimplementationでき है।API सेUI तक一貫した設計が得られるのが大きなメリット है।
विस्तार से जानने के लिएClaude Codeofficial documentationदेखें。
Related Posts
Claude Code से अपने Side Projects को Supercharge कैसे करें [Examples के साथ]
Claude Code से personal development projects को dramatically speed up करना सीखें। Real-world examples और idea से deployment तक practical workflow शामिल है।
Claude Code से Refactoring कैसे Automate करें
Claude Code से efficiently code refactoring automate करना सीखें। Real-world projects के लिए practical prompts और concrete refactoring patterns शामिल हैं।
Claude Code के साथ Complete CORS Configuration Guide
Claude Code का उपयोग करके complete CORS configuration guide सीखें। Practical tips और code examples शामिल हैं।