From 0da847872a18cfa06eee84799c982aca17c70cf9 Mon Sep 17 00:00:00 2001 From: Saminjay Goel <32176560+saminjay@users.noreply.github.com> Date: Mon, 30 Dec 2024 09:04:44 +0530 Subject: [PATCH 1/2] fix category --- src/components/CategoryList.tsx | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/components/CategoryList.tsx b/src/components/CategoryList.tsx index 5191fbd5..1cc4d5ab 100644 --- a/src/components/CategoryList.tsx +++ b/src/components/CategoryList.tsx @@ -1,25 +1,20 @@ +import { useEffect } from "react"; + import { useAppContext } from "../contexts/AppContext"; import { useCategories } from "../hooks/useCategories"; -let oldCategories = [] as string[]; - const CategoryList = () => { const { category, setCategory } = useAppContext(); const { fetchedCategories, loading, error } = useCategories(); + useEffect(() => { + setCategory(fetchedCategories[0]); + }, [fetchedCategories]); + if (loading) return
Loading...
; if (error) return
Error occured: {error}
; - // NOTE: set the first element as selected - // switching between categories - // Just a temporary solution. - // If you've better solution, I would appreciate it :) - if (oldCategories !== fetchedCategories) { - setCategory(fetchedCategories[0]); - oldCategories = fetchedCategories; - } - return (