Bird
0
0

You want to add a new feature to reset the counter state to zero using actions and reducers. Which of these is the best way to implement the reset action and update the reducer accordingly?

hard📝 Application Q15 of 15
Angular - State Management
You want to add a new feature to reset the counter state to zero using actions and reducers. Which of these is the best way to implement the reset action and update the reducer accordingly?
AAdd <code>case 'reset': state.count = 0; return state;</code> directly in reducer without action
BDefine <code>const reset = createAction('reset');</code> and add <code>case 'reset': return { count: 0 };</code> in reducer
CDefine <code>const reset = createAction('reset', () => 0);</code> and return 0 in reducer
DUse <code>dispatch({ type: 'reset', count: 0 })</code> and ignore reducer changes
Step-by-Step Solution
Solution:
  1. Step 1: Define the reset action properly

    Use createAction with a string type 'reset' to define the action.
  2. 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.
  3. Final Answer:

    Define const reset = createAction('reset'); and add case 'reset': return { count: 0 }; in reducer -> Option B
  4. Quick Check:

    Action + reducer case resets state immutably [OK]
Quick Trick: Create action and return new state in reducer [OK]
Common Mistakes:
  • Mutating state directly in reducer
  • Ignoring reducer update for new action
  • Misusing createAction with payload function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes