useMessages
useMessages returns the MessagesController, which manages the messages surfaced by the configurator: warnings, errors, and notices, both global and attribute-scoped. Reach for it when you want to read, display, add, or clear messages, or run their actions (such as reverting the last change).
Getting the controller
import { useMessages } from "@expivi/product-configurator";
const messages = useMessages(configurator);
End-to-end example
Render the global messages and let the user act on them:
import { useMessages, MessageType } from "@expivi/product-configurator";
const messages = useMessages(configurator);
// Keep a live view of global messages
const unsubscribe = messages.subscribeGlobal(items => {
items.forEach(m => console.log(m.type, m.message));
});
// Show a one-off warning (won't duplicate if already shown)
messages.showMessageOnce(MessageType.Warning, "Your selection was adjusted");
// Later, on teardown
unsubscribe();
API
data
Returns all current messages from the store as a Message[].
globalMessages
Returns only the messages that are not attached to a specific attribute. Attribute-specific messages are meant to be shown inline on their attribute components instead.
subscribe(callback)
Subscribes to all messages. The callback receives the full Message[]. Returns an unsubscribe function.
subscribeGlobal(callback)
Subscribes to global messages only (those without an attribute). Returns an unsubscribe function; call it during cleanup.
const unsubscribe = messages.subscribeGlobal(items => {
render(items);
});
unsubscribe();
add(message)
Pushes a new Message and assigns it a generated id.
messages.add({ type: MessageType.Error, message: "Something went wrong" });
clear()
Removes all messages.
clearById(id)
Removes a single message by its generated id.
clearByType(type)
Removes all messages of a given MessageType.
clearOnRuleEvaluation()
Removes messages flagged for clearing during rule evaluation. Messages with clearOnRuleEvaluation: false are preserved.
getAttributeMessages(identifier)
Returns the messages attached to a specific attribute identifier.
getMessagesByAction(actionType)
Returns the messages that expose a specific MessageActionType action (for example "revert").
clearByAction(actionType)
Clears every message that contains a matching action type.
executeAction(messageId, actionType)
Executes a message action, then removes the message. Currently supports "revert", which calls configurator.revertLastAction(). Returns a Promise.
await messages.executeAction(message.id, "revert");
showMessageOnce(type, message)
Adds a message only if an identical one (same type and text) is not already present, avoiding duplicates.