Skip to content

Commit ad227f2

Browse files
authored
Merge pull request #18 from saminjay/category-fix
Fixed CategoryList Component
2 parents 06abb98 + 35f6e65 commit ad227f2

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+
}, [setCategory, 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)