user2066880
user2066880

Reputation: 5034

Name for functions that deal with Redux state (e.g. reducers and selectors)

I need help naming functions that act on a slice of Redux state. What would be a good name for the type of functions that reducers and selectors are? I came up with stateHandler, but it's too wordy and generic.

Upvotes: 0

Views: 220

Answers (2)

Haim Zamir
Haim Zamir

Reputation: 351

If you insist on coming up with a term that handles both the "reducer" level and the "selector" level of this pattern, (and you don't like reducer, since it pretty much is reducers all the way down), you could consider the term "transformer" since you are taking an input and old state, and creating a transformed result.

Upvotes: 1

klugjo
klugjo

Reputation: 20875

As far as I am concerned these are also called reducers. Cf the documentation:

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.prototype.reduce(reducer, ?initialValue).

I sometimes add a prefix to specify what that reducer is: userReducer, productReducer and so on ...

Then within a reducer itself I will usually call the action handler function the same as the action name. For example:

function todos(state = [], action) {
  switch (action.type) {
    case 'ADD_TODO':
      return addTodo(state, action); // then put your addTodo function above or in a different file
    case 'TOGGLE_TODO':
      return toggleTodo(state, action); // idem
    default:
      return state;
  }
}

Upvotes: 0

Related Questions