From ec0477bfba622dc2ff605fa2d89bd226e4365044 Mon Sep 17 00:00:00 2001 From: akash Date: Wed, 17 Dec 2025 19:19:19 +0530 Subject: [PATCH] scroll top issue fixed --- src/components/isotope/CaseStudies.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/components/isotope/CaseStudies.js b/src/components/isotope/CaseStudies.js index d6ad18b..652bd63 100644 --- a/src/components/isotope/CaseStudies.js +++ b/src/components/isotope/CaseStudies.js @@ -1,6 +1,6 @@ "use client"; -import { useState, useEffect, useRef } from "react"; +import { useState, useEffect, useRef, useMemo } from "react"; import Image from "next/image"; import Link from "next/link"; import { PortfolioData } from "@/utils/constant.utils"; @@ -21,20 +21,30 @@ const CaseStudies = () => { const [visibleCount, setVisibleCount] = useState(6); const observerRef = useRef(null); - const filteredItems = - activeTab === "*" + // Memoize filtered items to prevent unnecessary re-computations + const filteredItems = useMemo(() => { + return activeTab === "*" ? PortfolioData : PortfolioData.filter((item) => item.category === activeTab); + }, [activeTab]); + // Reset visible count when tab changes useEffect(() => { setVisibleCount(6); }, [activeTab]); + // Infinite Scroll Observer useEffect(() => { const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting) { - setVisibleCount((prev) => prev + 6); + setVisibleCount((prev) => { + // Only increase if we haven't shown all items yet + if (prev < filteredItems.length) { + return prev + 6; + } + return prev; + }); } }, { threshold: 0.1 } @@ -49,7 +59,7 @@ const CaseStudies = () => { observer.unobserve(observerRef.current); } }; - }, [filteredItems]); + }, [filteredItems]); // Dependency ensures observer recreates only when list changes return ( <>