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
| Name | Type | Required | Description |
|---|---|---|---|
| idName | string | The name of the cookie or storage key (defaults to 'ncc'). |
Return value
| Property | Type | Description |
|---|---|---|
| cartId | string | null | The 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>
);
}