intercept
Registers an interceptor that fires before the store's default handler for a given action. If the interceptor returns false, the action is canceled and doesn't reach any downstream handlers. Interceptors can validate, modify, or block an action payload before it takes 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.getById(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");