0
0
Angularframework~10 mins

NgRx store concept in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - NgRx store concept
Component dispatches Action
Action reaches Reducer
Reducer updates State
Store holds new State
Component selects State slice
Component re-renders with new data
Shows how a component sends an action, the reducer updates the state, and the component gets updated state from the store.
Execution Sample
Angular
store.dispatch({ type: 'increment' });

const newState = reducer(oldState, { type: 'increment' });

store.select('counter').subscribe(value => console.log(value));
Dispatch an increment action, reducer updates state, component subscribes to updated counter value.
Execution Table
StepAction DispatchedOld StateReducer ResultNew State in StoreComponent Output
1increment{ counter: 0 }{ counter: counter + 1 }{ counter: 1 }1
2increment{ counter: 1 }{ counter: counter + 1 }{ counter: 2 }2
3decrement{ counter: 2 }{ counter: counter - 1 }{ counter: 1 }1
4reset{ counter: 1 }{ counter: 0 }{ counter: 0 }0
💡 No more actions dispatched, store state stable.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
counter01210
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.