Skip to content

Commit 0da8478

Browse files
committed
fix category
1 parent 286578b commit 0da8478

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/components/CategoryList.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1+
import { useEffect } from "react";
2+
13
import { useAppContext } from "../contexts/AppContext";
24
import { useCategories } from "../hooks/useCategories";
35

4-
let oldCategories = [] as string[];
5-
66
const CategoryList = () => {
77
const { category, setCategory } = useAppContext();
88
const { fetchedCategories, loading, error } = useCategories();
99

10+
useEffect(() => {
11+
setCategory(fetchedCategories[0]);
12+
}, [fetchedCategories]);
13+
1014
if (loading) return <div>Loading...</div>;
1115

1216
if (error) return <div>Error occured: {error}</div>;
1317

14-
// NOTE: set the first element as selected
15-
// switching between categories
16-
// Just a temporary solution.
17-
// If you've better solution, I would appreciate it :)
18-
if (oldCategories !== fetchedCategories) {
19-
setCategory(fetchedCategories[0]);
20-
oldCategories = fetchedCategories;
21-
}
22-
2318
return (
2419
<ul role="list" className="categories">
2520
{fetchedCategories.map((name, idx) => (

0 commit comments

Comments
 (0)