What is a Redux reducer?
Answer
A reducer is a pure function that takes the current state and an action, and returns the next state. Signature: (state, action) => newState. Key rules: No side effects: no API calls, no random values, no mutations. Immutability: return a new state object instead of mutating the existing one. Predictable: same inputs always produce the same output. Example: function todosReducer(state = [], action) { switch (action.type) { case "todos/added": return [...state, action.payload]; case "todos/removed": return state.filter(t => t.id !== action.payload); default: return state; } }. The default case returns current state for unknown actions — important because Redux initializes state by dispatching a dummy action. Immutability: Redux Toolkit's createReducer and createSlice use Immer under the hood, allowing "mutating" syntax that is actually converted to immutable updates: state.push(action.payload) inside RTK reducers is safe.