Performance: Actions and reducers pattern
This pattern affects how state changes trigger UI updates and how efficiently Angular processes those changes during user interactions.
Jump into concepts and practice - no test required
const reducer = (state, action) => { switch(action.type) { case 'UPDATE': return { ...state, data: action.payload }; default: return state; } };
const reducer = (state, action) => { switch(action.type) { case 'UPDATE': // heavy computation or deep cloning const newState = JSON.parse(JSON.stringify(state)); newState.data = action.payload; return newState; default: return state; } };
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy deep cloning in reducers | Triggers full component tree checks | Multiple reflows if many bindings update | High paint cost due to many DOM updates | [X] Bad |
| Shallow state updates with OnPush | Minimal DOM updates, only changed components | Single reflow per update | Low paint cost | [OK] Good |
action?{ type: 'increment' } if the initial state is { count: 0 }?
function counterReducer(state = { count: 0 }, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
}function todoReducer(state = [], action) {
if (action.type = 'add') {
return [...state, action.payload];
}
return state;
}const reset = createAction('reset'); and add case 'reset': return { count: 0 }; in reducer -> Option B