Overview
TheuseProduct hook fetches detailed information for a single product by its ID. It’s commonly used on product detail pages and includes automatic enabling/disabling based on ID availability.
Source: src/hooks/useProduct.js:4
Function Signature
Parameters
The numeric ID of the product to fetch
Return Value
Returns a React Query result object:Product object with detailed information
Product ID
Product title/name
Detailed product description
Product price
URL of the product thumbnail image
Array of product image URLs
Product category slug
Product rating (format varies by API)
Discount percentage if applicable
true when the product data is being fetchedtrue if an error occurred during fetchingError object if the request failed
Enabled Query Behavior
The query is automatically disabled when
id is falsy (via enabled: !!id). This prevents unnecessary API calls when the ID hasn’t been set yet.Usage Examples
URL Parameter Pattern
Common pattern when using with React Router:API Service
The hook usesproductService.getById(id) which makes a GET request to /products/{id}.
Source: src/services/productService.js:8
Caching Strategy
Each product is cached individually with the query key
["product", id]. This means:- Different products have separate cache entries
- Products remain cached even when navigating between pages
- Switching back to a previously viewed product shows cached data instantly
Error Handling
From the real implementation (src/pages/productDetail/ProductDetail.jsx:32):
Related Hooks
- useProducts - Fetch all products
- useProductsByCategory - Fetch products by category
