Skip to main content

useReviews

The useReviews hook is a specialized wrapper around useFetch used to fetch user reviews for a specific product. It manages cache keys automatically based on the productCode.

import { useReviews } from '@archibald/product';

const { data: reviews, isLoading, error } = useReviews(productCode, reviewOptions, fetchOptions);

Parameters

NameTypeRequiredDescription
productCodestring✔️The code of the product for which to fetch reviews.
reviewOptionsProductReviewsRequestOptionsOptions for the review request (e.g., max count).
fetchOptionsFetchOptionsStandard options for the useFetch hook.

productCode

The unique identifier of the product.

reviewOptions

  • Type: ProductReviewsRequestOptions
NameTypeDescription
fieldsstringDefines the depth of the returned review data.
maxCountnumberThe maximum number of reviews to return.

fetchOptions

Standard data fetching options. See FetchOptions for more information.

Return value

  • Type: FetchResult<ProductReviewsData>
PropertyTypeDescription
dataProductReviewsData | nullThe reviews data retrieved from the backend.
errorDefaultResponseError | nullAny error that occurred during the fetch.
isLoadingbooleanTrue if the request is currently in progress.
isDonebooleanTrue if the request has finished.
refetchFunctionA function to manually trigger a refresh.
requestDataRequestThe underlying DataRequest instance.

Example

import { useReviews } from '@archibald/product';

function ProductReviews({ productCode }) {
const { data, isLoading, error } = useReviews(productCode, { maxCount: 5 });

if (isLoading) return <div>Loading reviews...</div>;
if (error) return <div>Error loading reviews.</div>;

const reviews = data?.reviews || [];

return (
<div>
<h3>Customer Reviews ({reviews.length})</h3>
{reviews.map(review => (
<div key={review.id}>
<strong>Rating: {review.rating}</strong>
<p>{review.comment}</p>
<small>By: {review.alias}</small>
</div>
))}
</div>
);
}