JS Built-ins
redux-legacyreducerbeginner

Basic Reducer

πŸ“– Usage Example
function reducer(state, action) {
  if (action.type === 'INCREMENT') return state + 1
  return state
}
reducer(0, { type: 'INCREMENT' }) // β†’ 1
reducer(5, { type: 'NOOP' })      // β†’ 5

A reducer takes the current state and an action, and returns the next state without mutating anything.

Basic Reducer

A reducer is a pure function with the signature (state, action) => state. It receives the current state and an action object, and returns the next state without mutating anything.

function reducer(state, action) {
  if (action.type === 'INCREMENT') return state + 1
  return state
}

Challenge: Implement reducer(state, action) that returns state + 1 when the action type is 'INCREMENT', and returns the current state for any other action.

πŸ’‘ Hints (2)
  • β€’ Use an `if` statement or a `switch` to check `action.type`.
  • β€’ Always return a value β€” reducers must never return `undefined`.