diff --git a/components/main/detail/list-news.tsx b/components/main/detail/list-news.tsx index 80fcf10..9cb62d1 100644 --- a/components/main/detail/list-news.tsx +++ b/components/main/detail/list-news.tsx @@ -99,6 +99,8 @@ export default function ListNews() { null, ); + const requestIdRef = useRef(0); + const handleMonthClick = (monthIndex: number) => { setSelectedMonth(monthIndex); setSelectedDate(new Date(year, monthIndex, 1)); @@ -181,7 +183,7 @@ export default function ListNews() { const yearQ = searchParams.get("year"); const pageQ = searchParams.get("page"); - getArticle({ + await getArticle({ title: search, category: setup, month: month ? Number(month) : null, @@ -201,6 +203,8 @@ export default function ListNews() { year?: number | null; page?: number; }) { + requestIdRef.current += 1; + const currentRequestId = requestIdRef.current; loading(); const usedPage = props?.page || page; @@ -255,10 +259,19 @@ export default function ListNews() { timeStamp: getUnixTimestamp(), }; - const response = await getListArticle(req); - setArticle(response?.data?.data); - setTotalPage(response?.data?.meta?.totalPage); - close(); + try { + const response = await getListArticle(req); + + // ❗ GUARD: hanya request terakhir yang boleh set state + if (currentRequestId !== requestIdRef.current) return; + + setArticle(response?.data?.data); + setTotalPage(response?.data?.meta?.totalPage); + } finally { + if (currentRequestId === requestIdRef.current) { + close(); + } + } } const debounceTimeout = useRef(null);