Skip to main content

useProducts

The useProducts hook is a specialized wrapper around useFetch used to fetch multiple products by their codes. It automatically handles cache key generation and state management.

warning

This hook should be used with caution. For general product listing and searching, please use the Search module instead. This hook is intended for specific use cases like product aggregators or custom project requirements.

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

const { data: products, isLoading, error } = useProducts(productCodes, productOptions, fetchOptions);

Parameters

NameTypeRequiredDescription
productCodesstring[]✔️An array of product codes to fetch.
productOptionsProductRequestOptionsOptions for the product request.
fetchOptionsFetchMinOptionsStandard options for the useFetch hook.

productCodes

An array of strings, where each string is a unique product code.

productOptions

  • Type: ProductRequestOptions
NameTypeDescription
fieldsstringDefines the depth of the returned product data (e.g., 'BASIC', 'DEFAULT', 'FULL').

fetchOptions

Standard data fetching options. See FetchOptions for more information.

Return value

  • Type: FetchResult<Product[]>
PropertyTypeDescription
dataProduct[] | nullThe list of product 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 { useProducts } from '@archibald/product';

function RelatedProducts({ codes }) {
const { data: products, isLoading, error } = useProducts(codes, { fields: 'BASIC' });

if (isLoading) return <div>Loading related products...</div>;
if (error) return <div>Failed to load related products.</div>;

return (
<ul>
{products?.map(product => (
<li key={product.code}>{product.name}</li>
))}
</ul>
);
}