useOptimisticCart
The useOptimisticCart takes an existing cart object, processes all pending cart actions, and locally mutates the cart with optimistic state. An optimistic cart makes cart actions immediately render in the browser while actions sync to the server. This increases the perceived performance of the application.
Anchor to useOptimisticCart-parametersParameters
- Anchor to cartcartcartDefaultCartDefaultCart
The cart object from
context.cart.get()returned by a server loader.
A new cart object augmented with optimistic state for
linesand. Each cart line item that is optimistically added includes anproperty. Also if the cart has any optimistic state, a root propertywill be set totrue.
DefaultCart
Promise<CartReturn | null> | CartReturn | nullCartReturn
Cart & {
errors?: StorefrontApiErrors;
}Cart
- attributes
The cart's attributes.
{ __typename?: "Attribute"; key?: string; value?: string; }[] - buyerIdentity
The cart's buyer identity.
CartType['buyerIdentity'] - checkoutUrl
The checkout URL for the cart, if the cart has been created in the Storefront API.
string - cost
The cost for the cart, including the subtotal, total, taxes, and duties.
CartType['cost'] - discountCodes
The discount codes applied to the cart.
{ __typename?: "CartDiscountCode"; applicable?: boolean; code?: string; }[] - id
The cart's ID if it has been created through the Storefront API.
string - lines
The cart lines.
Array<CartLine | ComponentizableCartLine> - note
The cart's note.
string - totalQuantity
The total number of items in the cart, across all lines. If there are no lines, then the value is 0.
number
StorefrontApiErrors
JsonGraphQLError[] | undefinedJsonGraphQLError
- extensions
Reserved for implementors to extend the protocol however they see fit, and hence there are no additional restrictions on its contents.
{ [key: string]: unknown; } - locations
If an error can be associated to a particular point in the requested GraphQL document, it should contain a list of locations.
{ line: number; column: number; }[] - message
string - name
string - path
If an error can be associated to a particular field in the GraphQL result, it _must_ contain an entry with the key `path` that details the path of the response field which experienced the error. This allows clients to identify whether a null result is intentional or caused by a runtime error.
(string | number)[] - stack
string
OptimisticCart
T extends undefined | null
? // This is the null/undefined case, where the cart has yet to be created.
// But we still need to provide an optimistic cart object.
{
isOptimistic?: boolean;
lines: {
nodes: Array<OptimisticCartLine>;
};
totalQuantity?: number;
} & Omit<PartialDeep<CartReturn>, 'lines'>
: Omit<T, 'lines'> & {
isOptimistic?: boolean;
lines: {
nodes: Array<OptimisticCartLine<T>>;
};
totalQuantity?: number;
}OptimisticCartLine
T extends LikeACart
? T['lines']['nodes'][number] & {isOptimistic?: boolean}
: T & {isOptimistic?: boolean}LikeACart
- lines
{ nodes: unknown[]; }