Bird
0
0

Given this reducer:

medium📝 state output Q4 of 15
Angular - State Management
Given this reducer:
const initialState = { value: 10 };
function valueReducer(state = initialState, action) {
  switch(action.type) {
    case 'add':
      return { value: state.value + action.payload };
    case 'subtract':
      return { value: state.value - action.payload };
    default:
      return state;
  }
}

What will be the state after dispatching { type: 'add', payload: 5 }?
A{ value: 5 }
B{ value: 15 }
C{ value: 10 }
D{ value: 0 }
Step-by-Step Solution
Solution:
  1. Step 1: Initial state

    The initial state is { value: 10 }.
  2. Step 2: Action dispatched

    The action is { type: 'add', payload: 5 }.
  3. Step 3: Reducer logic

    For 'add', the reducer returns { value: state.value + action.payload }, so 10 + 5 = 15.
  4. Final Answer:

    { value: 15 } -> Option B
  5. Quick Check:

    Reducer updates state by adding payload [OK]
Quick Trick: Reducer returns new state with updated value [OK]
Common Mistakes:
  • Ignoring the payload value
  • Returning old state instead of updated
  • Confusing add and subtract cases

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes