intercept
Registers an interceptor that fires before the store's default handler for a given action. Returning false from the interceptor cancels the action and prevents it from reaching any downstream handlers. Use this to validate, mutate, or block action payloads before they take effect.
Usage
api.intercept(
action: keyof StoreActions,
handler: (data: StoreActions[action]) => void | boolean | Promise<boolean>,
config?: { tag?: string | number | symbol }
): void;
| Parameter | Type | Description |
|---|---|---|
action | keyof StoreActions | Action name to intercept (e.g. "add-card", "move-card") |
handler | (data) => void | boolean | Promise<boolean> | Interceptor function. Return false to cancel the action. Modify data properties in place to alter the payload before it reaches the store. |
config | { tag? } | Optional. Assign a tag so the interceptor can be removed later with detach. |
Example
Block deletion of cards marked as locked:
api.intercept("delete-card", data => {
const state = api.getState();
const card = state.cards.byId(data.id);
if (card?.locked) return false;
});
Mutate the payload before the store processes it:
api.intercept("add-card", data => {
data.card.created = new Date();
});
Use a tag to remove the interceptor later:
api.intercept("move-card", handler, { tag: "move-guard" });
// later
api.detach("move-guard");