reducerbeginnerfunction reducer(state, action) {
if (action.type === 'INCREMENT') return state + 1
return state
}
reducer(0, { type: 'INCREMENT' }) // β 1
reducer(5, { type: 'NOOP' }) // β 5A reducer takes the current state and an action, and returns the next state without mutating anything.
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.