Skip to main content

useCartId

The useCartId hook provides access to the persistent cart identifier (cookie-based on web, storage-based on mobile). It ensures that guest carts persist across sessions and that the correct identifier is used when fetching or mutating cart data.

import { useCartId } from '@archibald/cart';

const { cartId, setCartId, clearCartId } = useCartId(idName);

Parameters

NameTypeRequiredDescription
idNamestringThe name of the cookie or storage key (defaults to 'ncc').

Return value

PropertyTypeDescription
cartIdstring | nullThe current cart ID.
setCartId(id: string) => Promise<void>Manually set the cart ID.
clearCartId() => Promise<void>Removes the cart ID from persistence.

Example

import { useCartId } from '@archibald/cart';

function DebugCartId() {
const { cartId, clearCartId } = useCartId();

return (
<div style={{ border: '1px solid red', padding: '10px' }}>
<strong>Debug - Current Cart ID:</strong> {cartId || 'No active cart'}
<button onClick={clearCartId}>Force Reset Cart ID</button>
</div>
);
}