Skip to main content

useStoreAttributes

useStoreAttributes returns a StoreAttributesController, the collection-level entrypoint for looking up, subscribing to, and (de)serializing every attribute in the store.

Getting the controller​

import { useStoreAttributes } from "@expivi/product-configurator";

const attributes = useStoreAttributes(configurator);

End-to-end example​

Find an attribute, react to list changes, then clean up:

import { useStoreAttributes, useAttribute } from "@expivi/product-configurator";

const attributes = useStoreAttributes(configurator);

const color = attributes.findByIdentifier("color");
if (color) {
const controller = useAttribute(configurator, color.cid);
console.log(controller.data.identifier);
}

const unsubscribe = attributes.subscribe(list => {
console.log(`${list.length} attributes in store`);
});

// later
unsubscribe();

API​

data​

Returns all attributes currently in the store.

subscribe(callback)​

Subscribes to the attribute list. Returns an unsubscribe function.

findByIdentifier(identifier)​

Finds one attribute by its business identifier, or returns null.

exists(cid)​

Returns true when an attribute with the given runtime cid exists.

findById(id, productId?)​

Finds an attribute by persistent id. If productId is provided, the search is scoped to that product.

filterByProductId(productId)​

Returns every attribute belonging to the given product id.

getBundleAttributesByProductCid(productCid)​

Returns the bundle attributes for a product, looked up by runtime productCid.

getPerProductAttributesByProductCid(productCid)​

Returns the per-product attributes (non-bundle, mode === "per_product") for a product, looked up by runtime productCid.

serialize()​

Serializes every attribute by delegating to each attribute controller's serialize().

parse(data)​

Parses serialized product attributes back into the store. Matching is done by persistent attribute id (not runtime cid); unmatched entries are skipped with a warning.

subscribe(...) returns an unsubscribe function. Call it when you no longer need updates to release the store subscription.