Entry & exit actions
While the statechart is running, it can execute other effects called actions.
An action can be fired upon entry or exit of a state. Actions are “fire-and-forget effects”; once the machine has fired the action, it moves on and forgets the action. You can also fire actions on transitions.
You can fire multiple entry and exit actions on a state. Top-level final states cannot have exit actions, since the machine is stopped and no further transitions can occur.
View this machine in the Stately Studio.
In our video player, we have entry and exit actions on the Playing state. We use the entry action of playVideo to fire an effect playing the video on entry to the Playing state. We use the exit action of pauseVideo to fire an effect pausing the video when the Playing state is exited.
Add an entry action to a state
- Select the state you want to have an entry action.
- Open the State details panel from the right tool menu.
- Use the + plus icon alongside the Entry actions to add a new action.
- Use the Custom tab under Entry actions to input the type for the entry action.
- Use the Assign tab under Entry actions to assign key and assignment pairs to the entry action.
- Save the entry action using the Save button.
Add an exit action to a state
- Select the state you want to have an exit action.
- Open the State details panel from the right tool menu.
- Use the + plus icon alongside the Exit actions to add a new action.
- Use the Custom tab under Exit actions to input the type for the exit action.
- Use the Assign tab under Exit actions to assign key and assignment pairs to the exit action.
- Save the entry action using the Save button.