File tree Expand file tree Collapse file tree 1 file changed +6
-11
lines changed Expand file tree Collapse file tree 1 file changed +6
-11
lines changed Original file line number Diff line number Diff line change 1+ import { useEffect } from "react" ;
2+
13import { useAppContext } from "../contexts/AppContext" ;
24import { useCategories } from "../hooks/useCategories" ;
35
4- let oldCategories = [ ] as string [ ] ;
5-
66const 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 ) => (
You can’t perform that action at this time.
0 commit comments