Discover how a single source of truth can save your app from chaos!
Why NgRx store concept in Angular? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a large Angular app where many parts need to share and update data like user info, settings, or shopping cart items.
You try to pass data manually through many components or use simple services to keep track.
Manually passing data around or using services can get messy fast.
It's easy to lose track of where data changes, causing bugs and inconsistent views.
Debugging becomes hard because state changes happen everywhere without a clear flow.
NgRx store gives you one central place to keep your app's state.
It uses a clear pattern where state changes happen only through defined actions and reducers.
This makes data flow predictable, easier to debug, and components stay in sync automatically.
this.cartService.addItem(item); this.cartItems = this.cartService.getItems();
store.dispatch(addItem({ item }));
this.cartItems$ = store.select(selectCartItems);It enables building scalable apps with predictable state management and easy debugging.
Think of an online store where the shopping cart updates instantly everywhere you look, no matter which page or component you're on.
Manual data sharing gets complicated and error-prone in big apps.
NgRx store centralizes state with clear rules for updates.
This leads to predictable, maintainable, and bug-resistant apps.
Practice
NgRx Store in an Angular application?Solution
Step 1: Understand NgRx Store role
The NgRx Store is designed to hold the application state in one place.Step 2: Compare with other options
Options B, C, and D describe other Angular features, not the store's purpose.Final Answer:
To keep all application data in one central place for easy access and updates -> Option CQuick Check:
NgRx Store = Central app data storage [OK]
- Confusing store with routing or HTTP services
- Thinking store manages styles or UI directly
loadItems using NgRx Store in a component?Solution
Step 1: Recall NgRx dispatch syntax
Actions are dispatched by callingthis.store.dispatch(action()).Step 2: Check other options for syntax errors
Options A, B, and C use incorrect methods or assignment instead of dispatch call.Final Answer:
this.store.dispatch(loadItems()); -> Option DQuick Check:
Dispatch action = this.store.dispatch(action()) [OK]
- Using emit or call instead of dispatch
- Assigning dispatch instead of calling it
const initialState = { count: 0 };
function counterReducer(state = initialState, action) {
switch(action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
}What will be the state after dispatching
{ type: 'increment' } twice starting from initial state?Solution
Step 1: Analyze reducer behavior for 'increment'
Each 'increment' action adds 1 to the current count.Step 2: Apply two increments starting from 0
0 + 1 = 1 after first increment, then 1 + 1 = 2 after second increment.Final Answer:
{ count: 2 } -> Option BQuick Check:
Two increments = count 2 [OK]
- Counting only one increment
- Confusing decrement with increment
function todoReducer(state = [], action) {
if (action.type === 'add') {
state.push(action.payload);
return state;
}
return state;
}Solution
Step 1: Check state mutation
The reducer usesstate.push(), which changes the original array directly.Step 2: Understand NgRx immutability rule
Reducers must return new state objects without mutating the old state.Final Answer:
Mutating state directly instead of returning a new state -> Option AQuick Check:
Reducers must be pure and immutable [OK]
- Using push instead of spread operator
- Ignoring immutability in reducers
1. Define interface UserProfileState { name: string; age: number; }
2. Create reducer userProfileReducer
3. Register feature state with key 'userProfile'
4. Select user name from storeWhich code snippet correctly selects the user name?Solution
Step 1: Understand feature selector usage
UsecreateFeatureSelectorwith the feature key to get the feature state.Step 2: Create selector for user name
UsecreateSelectorwith the feature selector and a projector function to select the name.Final Answer:
const selectUserProfile = createFeatureSelector<UserProfileState>('userProfile'); const selectUserName = createSelector(selectUserProfile, state => state.name); -> Option AQuick Check:
Feature selector + createSelector = correct pattern [OK]
- Passing string directly to createSelector
- Using pipe on selector instead of RxJS operators in component
