What are they?
The idea for this started by me not understanding
- Need to iterate over an array? – we can use
- Need to iterate and return data for each element? – you use
- Need to calculate a single value based on the array? – use the
The function is applied to all array elements one after another and “carries on” its result to the next call.
- accumulator – is the result of the previous function call, equals initial the first time (if initial is provided).
- item – is the current array item.
- index – is its position.
- array – is the array.
The first argument stores the combined result of all previous executions. And at the end it becomes the result of
Okay, but the React
useReducer hook looks like this:
(Curious about React useState and useReducer? I made a simple example where I refactor multiple
useState functions into a single
const  = … is interesting… what’s that doing again?
Here’s another example of a reducer