Dispatch an increment action, reducer updates state, component subscribes to updated counter value.
Execution Table
Step
Action Dispatched
Old State
Reducer Result
New State in Store
Component Output
1
increment
{ counter: 0 }
{ counter: counter + 1 }
{ counter: 1 }
1
2
increment
{ counter: 1 }
{ counter: counter + 1 }
{ counter: 2 }
2
3
decrement
{ counter: 2 }
{ counter: counter - 1 }
{ counter: 1 }
1
4
reset
{ counter: 1 }
{ counter: 0 }
{ counter: 0 }
0
💡 No more actions dispatched, store state stable.
Variable Tracker
Variable
Start
After 1
After 2
After 3
After 4
counter
0
1
2
1
0
Key Moments - 3 Insights
Why does the component get updated after dispatching an action?
Because the store holds the new state after the reducer runs, and the component subscribes to the store's state slice, so it re-renders with the updated value (see execution_table steps 1-4).
What happens if the reducer does not handle an action type?
The reducer returns the old state unchanged, so the store state stays the same and the component output does not change (not shown in table but implied).
Why is the state immutable in NgRx?
Because the reducer returns a new state object instead of modifying the old one, ensuring predictable state changes and easier debugging (see reducer result column in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the counter value in the store after step 2?
A2
B1
C0
D3
💡 Hint
Check the 'New State in Store' column at step 2 in the execution_table.
At which step does the counter value decrease?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action Dispatched' and 'New State in Store' columns to find when counter goes down.
If the 'reset' action was not handled by the reducer, what would be the counter value after step 4?
A0
B1
C2
DUndefined
💡 Hint
Refer to the key_moments about reducer behavior when action is not handled.
Concept Snapshot
NgRx Store Concept:
- Components dispatch actions to signal events.
- Reducers receive actions and return new state.
- Store holds the current state immutably.
- Components select state slices and update automatically.
- State changes flow is unidirectional and predictable.
Full Transcript
NgRx store works by components sending actions to the store. The store passes these actions to reducers, which create a new state based on the action type. The store then updates its state immutably. Components subscribe to parts of the store state and re-render when the state changes. This flow ensures a clear, predictable way to manage app data.