0
0
Angularframework~10 mins

Effects for side effects in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Effects for side effects
Action Dispatched
Effect Listens
Perform Side Effect
Dispatch New Action
Store Updates State
UI Reacts to State Change
When an action happens, an effect listens and runs a side effect like fetching data, then sends a new action to update the state and UI.
Execution Sample
Angular
readonly loadData$ = createEffect(() =>
  this.actions$.pipe(
    ofType(loadData),
    switchMap(() => this.api.getData()),
    map(data => loadDataSuccess({ data }))
  )
);
This effect listens for 'loadData' action, fetches data from API, then dispatches 'loadDataSuccess' with the data.
Execution Table
StepAction DispatchedEffect TriggeredSide EffectNew Action DispatchedStore Update
1loadDataYesAPI call startedNoNo
2API call resolvesNoData receivedloadDataSuccess({ data })No
3loadDataSuccess({ data })NoNo side effectNoState updated with data
4UI updatesNoNoNoUI shows new data
5No more actionsNoNoNoIdle
ExitNo new actionsNoNoNoEnd of effect cycle
💡 No new actions dispatched, effect cycle ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
actions$idleloadData dispatchedAPI response receivedloadDataSuccess dispatchedidle
state.dataemptyemptyemptyfilled with API datafilled with API data
UIempty viewloading indicatorloading indicatordata displayeddata displayed
Key Moments - 3 Insights
Why does the effect dispatch a new action instead of directly updating the state?
Effects only handle side effects and produce new actions; the store updates state when it receives these actions, as shown in steps 2 and 3 of the execution_table.
What happens if the API call fails inside the effect?
You would handle errors by dispatching a failure action inside the effect, similar to how loadDataSuccess is dispatched, but with error info. This is not shown here but follows the same pattern.
Does the effect run on every action?
No, it only runs when the specific action it listens for is dispatched, like loadData in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what action triggers the effect to start the API call?
AloadDataSuccess
BloadData
CAPI response received
DUI updates
💡 Hint
Check Step 1 in the execution_table where the effect triggers on an action.
At which step does the store update its state with the new data?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'State updated with data' in the Store Update column.
If the effect did not dispatch a new action after the API call, what would happen?
AThe store would update automatically
BThe UI would show loading forever
CThe state would not update with new data
DThe effect would run twice
💡 Hint
Refer to how the store updates only after receiving a new action in the execution_table.
Concept Snapshot
Effects listen for specific actions.
They run side effects like API calls.
After side effects, they dispatch new actions.
Store updates state on these new actions.
UI reacts to state changes.
This keeps side effects separate from state logic.
Full Transcript
In Angular, effects listen for actions dispatched to the store. When an action like 'loadData' is dispatched, the effect triggers and performs a side effect such as calling an API. Once the API responds, the effect dispatches a new action, for example 'loadDataSuccess', carrying the data. The store listens for this new action and updates its state accordingly. The UI then updates to reflect the new state. This flow separates side effects from state management, keeping code clean and predictable.