Claude Code के साथ Infinite Scroll कैसे Implement करें
Claude Code का उपयोग करके infinite scroll implement करना सीखें। Practical code examples और step-by-step guidance शामिल है।
Infinite Scroll का Mechanism
Infinite scroll एक ऐसा UI pattern है जिसमें जब user page के नीचे की तरफ पहुंचता है, तो अगला data automatically load हो जाता है। Social media और news apps में यह common है, लेकिन performance और accessibility को ध्यान में रखते हुए इसे implement करना अक्सर मुश्किल हो जाता है। Claude Code के साथ, आप best practices के अनुसार implementation जल्दी build कर सकते हैं।
Intersection Observer के साथ Basic Implementation
> Intersection Observer का उपयोग करके infinite scroll के लिए React hook बनाओ।
> Loading state और error handling भी include करो।
import { useEffect, useRef, useState, useCallback } from 'react';
interface UseInfiniteScrollOptions<T> {
fetchFn: (page: number) => Promise<{ data: T[]; hasMore: boolean }>;
initialPage?: number;
threshold?: number;
}
function useInfiniteScroll<T>({ fetchFn, initialPage = 1, threshold = 0.8 }: UseInfiniteScrollOptions<T>) {
const [items, setItems] = useState<T[]>([]);
const [page, setPage] = useState(initialPage);
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(true);
const [error, setError] = useState<Error | null>(null);
const observerRef = useRef<IntersectionObserver | null>(null);
const lastElementRef = useCallback(
(node: HTMLElement | null) => {
if (loading) return;
if (observerRef.current) observerRef.current.disconnect();
observerRef.current = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && hasMore) {
setPage((prev) => prev + 1);
}
},
{ threshold }
);
if (node) observerRef.current.observe(node);
},
[loading, hasMore, threshold]
);
useEffect(() => {
let cancelled = false;
setLoading(true);
setError(null);
fetchFn(page)
.then((result) => {
if (!cancelled) {
setItems((prev) => [...prev, ...result.data]);
setHasMore(result.hasMore);
}
})
.catch((err) => { if (!cancelled) setError(err); })
.finally(() => { if (!cancelled) setLoading(false); });
return () => { cancelled = true; };
}, [page, fetchFn]);
return { items, loading, hasMore, error, lastElementRef };
}
Component में उपयोग का उदाहरण
function ArticleList() {
const fetchArticles = useCallback(async (page: number) => {
const res = await fetch(`/api/articles?page=${page}&limit=20`);
const data = await res.json();
return { data: data.articles, hasMore: data.hasMore };
}, []);
const { items, loading, hasMore, error, lastElementRef } = useInfiniteScroll({
fetchFn: fetchArticles,
});
return (
<div role="feed" aria-busy={loading} aria-label="Articles list">
{items.map((article, index) => (
<article
key={article.id}
ref={index === items.length - 1 ? lastElementRef : null}
aria-posinset={index + 1}
aria-setsize={hasMore ? -1 : items.length}
>
<h2>{article.title}</h2>
<p>{article.summary}</p>
</article>
))}
{loading && <div aria-live="polite">Loading...</div>}
{error && <div role="alert">Error हो गया। फिर से try करें।</div>}
{!hasMore && <p>सभी articles display हो गए।</p>}
</div>
);
}
Backend पर Cursor-Based Pagination
// Offset-based के बजाय cursor-based का performance बेहतर है
app.get('/api/articles', async (req, res) => {
const limit = Math.min(Number(req.query.limit) || 20, 100);
const cursor = req.query.cursor as string | undefined;
const where = cursor ? { id: { gt: cursor } } : {};
const articles = await prisma.article.findMany({
where,
take: limit + 1,
orderBy: { id: 'asc' },
});
const hasMore = articles.length > limit;
const data = hasMore ? articles.slice(0, -1) : articles;
res.json({
data,
hasMore,
nextCursor: hasMore ? data[data.length - 1].id : null,
});
});
Summary
Claude Code का उपयोग करके, Intersection Observer-based infinite scroll से लेकर cursor-based API तक, सब कुछ consistently build किया जा सकता है। बड़ी मात्रा में data के rendering performance के लिए virtual scroll implementation और pagination methods की comparison के लिए pagination implementation देखें।
Intersection Observer API के details के लिए MDN Web Docs देखें।
अपने Claude Code वर्कफ़्लो को अगले स्तर पर ले जाएँ
Claude Code में तुरंत कॉपी-पेस्ट करने योग्य 50 आज़माए हुए प्रॉम्प्ट टेम्पलेट।
मुफ़्त PDF: 5 मिनट में Claude Code चीटशीट
मुख्य कमांड, शॉर्टकट और प्रॉम्प्ट उदाहरण एक प्रिंट योग्य पृष्ठ पर।
लेखक के बारे में
Masa
Claude Code का गहराई से उपयोग करने वाले इंजीनियर। claudecode-lab.com चलाते हैं, जो 10 भाषाओं में 2,000 से अधिक पेजों वाला टेक मीडिया है।
संबंधित लेख
Claude Code में कस्टम स्लैश कमांड बनाना — अपना वर्कफ़्लो
Claude Code में कस्टम स्लैश कमांड बनाने का तरीका सीखें। फ़ाइल स्थान, आर्ग्युमेंट्स और बार-बार के कार्यों का स्वचालन कोड उदाहरणों के साथ।
Claude Code से Productivity 3 गुना बढ़ाने की 10 Tips
Claude Code से ज़्यादा पाने की 10 practical tips जानें। Prompt strategies से workflow shortcuts तक, ये techniques आज से ही आपकी efficiency boost करेंगी।
Claude Code के साथ Canvas/WebGL Optimization
Claude Code का उपयोग करके Canvas/WebGL optimization के बारे में जानें। Practical tips और code examples शामिल हैं।