Overview
TheuseProducts hook is a React Query wrapper that fetches all products from the e-commerce API. It handles caching, loading states, and automatic refetching.
Source: src/hooks/useProducts.js:5
Function Signature
Parameters
This hook takes no parameters.Return Value
Returns a React Query result object with the following properties:Product data returned from the API
Array of product objects (up to 200 products)
true when the initial data is being fetchedtrue if an error occurred during fetchingError object if the request failed
Function to manually refetch the products data
Usage Example
API Service
The hook usesproductService.getAll() which makes a GET request to /products?limit=200.
Source: src/services/productService.js:4
Caching
Products are cached with the query key
["products"]. React Query automatically:- Caches the response
- Deduplicates requests
- Refetches stale data in the background
- Shares data across components
Related Hooks
- useProduct - Fetch a single product by ID
- useProductsByCategory - Fetch products filtered by category
useProductsByCategory
The same file also exportsuseProductsByCategory for fetching products filtered by category:
Parameters
The category slug to filter products by
Behavior
The query is disabled when
category is empty or undefined (via enabled: !!category)