useFilterDropdown
This custom hook is designed to fetch tag data and handle URL query parameters for filtering in a dropdown menu.
// useFilterDropdown.js
import useSWR from 'swr';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
export const useFilterDropdown = () => {
const router = useRouter();
const { locale } = router;
const { data: normalTagsData, isLoading: isNormalTagsDataLoading } = useSWR(
// Fetching logic here...
);
useEffect(() => {
const defaultId = normalTagsData?.[0]?.id;
const hasFilterQuery = router.query[queryNameMap.get('gameListFilterHotOrNew')];
if (!isNormalTagsDataLoading && defaultId && !hasFilterQuery) {
router.push({
query: {
...router.query,
[queryNameMap.get('gameListFilterHotOrNew')]: defaultId,
},
});
}
}, [isNormalTagsDataLoading, normalTagsData, router.query]);
return { normalTagsData, isNormalTagsDataLoading };
};
FilterDropdownGameList
This component uses the useFilterDropdown
custom hook to fetch and display tag data in a dropdown filter.
import { useFilterDropdown } from './useFilterDropdown';
export function FilterDropdownGameList(prop: FilterDropdownGameListProps) {
const { normalTagsData, isNormalTagsDataLoading } = useFilterDropdown();
// Rest of your component logic
}