Performance: NgRx store concept
NgRx store affects how state changes impact rendering and user interaction responsiveness in Angular apps.
Jump into concepts and practice - no test required
this.store.select(selectFeatureSlice).subscribe(data => { this.featureData = data; });
this.store.select(state => state).subscribe(data => { this.fullState = data; this.cd.detectChanges(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Selecting entire state | Many components update | Many reflows | High paint cost | [X] Bad |
| Selecting feature slice only | Minimal DOM updates | Few reflows | Low paint cost | [OK] Good |
NgRx Store in an Angular application?loadItems using NgRx Store in a component?this.store.dispatch(action()).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;
}
}{ type: 'increment' } twice starting from initial state?function todoReducer(state = [], action) {
if (action.type === 'add') {
state.push(action.payload);
return state;
}
return state;
}state.push(), which changes the original array directly.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?createFeatureSelector with the feature key to get the feature state.createSelector with the feature selector and a projector function to select the name.