For a feature to transition from one state to another a dispatch has to happen. A dispatch can either be an action from within the feature, from another component or from the environment.
/*
Actions triggered within the feature
*/
export type PublicAction = {
type: 'SIGN_IN'
}
/*
The actions that can be triggered from outside the
feature
*/
export type PrivateAction = {
type: 'ERROR_TIMEOUT'
}
Actions has a type property which describes something that happened in the application. This could be a direct user interaction, a consequence of resolving an environment effect or something else.
Ensuring valid transitions
You want to be exact about a transition, which is why we always add the return type for every possible action inside the reducer. Without this return type, TypeScript will not yell when you add unnecessary properties, which can lead to bugs, especially when spreading existing state. The createReducer factory is where the transitions are expressed.
You have to define all the states and optionally what actions they respond to. You can define the same action in multiple states, even cause a different transition based on the current state. The transition handlers receives two arguments:
The first argument is the action, in this case the SIGN_IN action. The second argument is the current state, in this case SIGNED_OUT. You can use these two arguments to build the new state.
Command
Even though most effects can be expressed as entering a new state, there are many scenarios where you want to run an effect within the same state or conditionally when entering a new state. You can use Commands for this purpose.