Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Redux's state functions called reducers?

This is a part the official Redux documentation:

It’s called a reducer because it’s the type of function you would pass to Array.prototype.reduce(reducer, ?initialValue)

It doesn't make much sense to me. Could somebody explain to me why they are actually called reducers? The fact that they return a default value (or they have a default argument value) doesn't make them reducers IMHO.

like image 853
Anton Savchenko Avatar asked Dec 19 '15 22:12

Anton Savchenko


People also ask

Why are reducers called reducers?

The reducer is a pure function that takes the previous state and an action, and returns the next state. (previousState, action) => newState. It's called a reducer because it's the type of function you would pass to Array.

What is a reducer function?

Reducers are functions that take the current state and an action as arguments, and return a new state result. In other words, (state, action) => newState .

What does reducer mean in programming?

A reducer is a function that determines changes to an application's state. It uses the action it receives to determine this change. We have tools, like Redux, that help manage an application's state changes in a single store so that they behave consistently.

What does reducer mean in Redux?

In Redux, a reducer is a pure function that takes an action and the previous state of the application and returns the new state. The action describes what happened and it is the reducer's job to return the new state based on that action.


1 Answers

The fact that they return a default value (or they have a default argument value) doesn't make them reducers IMHO.

Reducers do not just return default values. They always return the accumulation of the state (based on all previous and current actions).

Therefore, they act as a reducer of state. Each time a redux reducer is called, the state is passed in with the action (state, action). This state is then reduced (or accumulated) based on the action, and then the next state is returned. This is one cycle of the classic fold or reduce function.

As @azium summed up with state -> action -> state.

like image 97
Davin Tryon Avatar answered Sep 20 '22 19:09

Davin Tryon