api.exec(). You can listen to events to react to changes, or intercept them to validate and cancel operations.
Listening to events with api.on()
api.on() registers a callback that fires after an action has been applied to the state. Use it to react to changes, sync to a backend, or update external UI.
on<ActionName> naming convention:
Event prop names are derived by removing hyphens from the action name and prepending
on. For example, "add-task" becomes onaddtask.Intercepting events with api.intercept()
api.intercept() registers a callback that fires before the action is processed. Return false from the callback to cancel the action. Any other return value (including undefined) allows the action to proceed.
Difference between on() and intercept()
Cancelling an action
Returnfalse from an intercept callback to cancel the action. The state is not modified and no on() callbacks fire.
Grouping and removing listeners
Pass atag to group related listeners. Call api.detach(tag) to remove all listeners in the group at once.