Discover how a simple pattern can save you hours of debugging and confusion in your app!
Why Actions and reducers pattern in Angular? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a large app where many parts change data, like a shopping cart or user profile, and you try to update everything manually.
You write code everywhere to change data and keep UI in sync.
Manual updates get messy fast.
It's easy to forget to update some parts, causing bugs.
Tracking changes and fixing bugs becomes a nightmare.
The actions and reducers pattern organizes changes clearly.
Actions describe what happened, reducers decide how data changes.
This keeps data updates predictable and easy to follow.
cart.items.push(newItem); updateUI();
dispatch(addItem(newItem)); // reducer updates state automatically
This pattern makes managing app data simple, predictable, and bug-resistant, even as apps grow big.
In an online store, adding an item triggers an action, the reducer updates the cart state, and the UI updates automatically without extra code.
Manual data updates get complicated and error-prone.
Actions and reducers separate what happens from how data changes.
This leads to clearer, easier-to-maintain app state management.
Practice
action?Solution
Step 1: Understand the purpose of actions
Actions are simple objects that describe an event that happened in the app and carry any necessary data.Step 2: Differentiate from other parts
Reducers handle state changes, not actions. UI updates and data fetching are separate concerns.Final Answer:
To describe what happened and carry data about the event -> Option AQuick Check:
Action = event description + data [OK]
- Confusing actions with reducers
- Thinking actions update UI directly
- Assuming actions hold the whole state
Solution
Step 1: Recall createAction syntax
The correct syntax is calling createAction with a string describing the action type.Step 2: Check each option
const loadItems = createAction('Load Items'); matches the correct syntax. const loadItems = createAction = 'Load Items'; uses wrong assignment. const loadItems = actionCreate('Load Items'); uses wrong function name. const loadItems = createAction('Load Items', payload); incorrectly adds a second argument without proper structure.Final Answer:
const loadItems = createAction('Load Items'); -> Option DQuick Check:
createAction('type') is correct [OK]
- Using wrong function names
- Assigning createAction instead of calling it
- Passing payload directly as second argument
{ 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;
}
}Solution
Step 1: Identify the action type and initial state
The action type is 'increment' and initial state has count 0.Step 2: Follow reducer logic for 'increment'
The reducer returns a new state with count increased by 1, so count becomes 1.Final Answer:
{ count: 1 } -> Option AQuick Check:
increment adds 1 to count [OK]
- Returning old state instead of updated
- Confusing increment with decrement
- Expecting mutation instead of new object
function todoReducer(state = [], action) {
if (action.type = 'add') {
return [...state, action.payload];
}
return state;
}Solution
Step 1: Check the if condition syntax
The condition uses single equals (=) which assigns instead of compares. This causes a bug.Step 2: Verify other parts
Default case is handled by returning state. State as array is valid for todo list. Returning new array is correct for immutability.Final Answer:
Using assignment (=) instead of comparison (===) in the if condition -> Option CQuick Check:
Use '===' for comparison in conditions [OK]
- Confusing '=' with '===' in conditions
- Thinking default case is missing
- Believing state must be an object
Solution
Step 1: Define the reset action properly
Use createAction with a string type 'reset' to define the action.Step 2: Update reducer to handle reset
Add a case for 'reset' that returns a new state object with count set to 0, ensuring immutability.Final Answer:
Defineconst reset = createAction('reset');and addcase 'reset': return { count: 0 };in reducer -> Option BQuick Check:
Action + reducer case resets state immutably [OK]
- Mutating state directly in reducer
- Ignoring reducer update for new action
- Misusing createAction with payload function
