Overview
TheuseCategories hook is a React Query wrapper that fetches all available product categories from the API. It provides category data for filtering and navigation.
Source: src/hooks/useCategories.js:4
Function Signature
Parameters
This hook takes no parameters.Return Value
Returns a React Query result object:true when the initial data is being fetchedtrue if an error occurred during fetchingError object if the request failed
Usage Example
Default Value Pattern
Notice the destructuring pattern
{ data: categories = [] } - this provides an empty array as the default value when data is still loading or undefined, preventing runtime errors when mapping over categories.API Service
The hook usescategoryService.getAll() which fetches categories from the API endpoint.
Source: src/services/categoryService.js
Caching
Categories are cached with the query key
["categories"]. Since categories rarely change, this data is efficiently shared across all components that need it.Real-World Example
From the Filters component (src/components/filters/Filters.jsx:112):
Related Hooks
- useProducts - Fetch all products
- useProductsByCategory - Fetch products filtered by category
