What are reducers in Redux?

Beginner

Answer

Reducers are pure functions that take the current state and an action, and return a new state. They specify how the application's state changes in response to actions.

const todosReducer = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return [...state, action.payload];
    case 'REMOVE_TODO':
      return state.filter(todo => todo.id !== action.payload.id);
    default:
      return state;
  }
};