reduxjs / reselect

Selector library for Redux
MIT License
19.04k stars 670 forks source link

Conflicting examples in the docs #629

Closed evandavis closed 1 year ago

evandavis commented 1 year ago

The documentation as written creates confusion around when and when not to use memoization. Specifically, the section Balance Selector Usage contains the following code:

// ❌ DO NOT memoize: will always return a consistent reference
const selectTodos = state => state.todos
const selectNestedValue = state => state.some.deeply.nested.field
const selectTodoById = (state, todoId) => state.todos[todoId]

// ❌ DO NOT memoize: deriving data, but will return a consistent result
const selectItemsTotal = state => {
  return state.items.reduce((result, item) => {
    return result + item.total
  }, 0)
}
const selectAllCompleted = state => state.todos.every(todo => todo.completed)

This makes sense to me! However, the Create Selector Overview shows the following examples:

const selectABC = createSelector([selectA, selectB, selectC], (a, b, c) => {
  // do something with a, b, and c, and return a result
  return a + b + c
})
const selectA = state => state.a
const selectB = state => state.b

const selectA1 = createSelector([selectA], a => a.first)

const selectResult = createSelector([selectA1, selectB], (a1, b) => {
  console.log('Output selector running')
  return a1 + b
})

These examples seem to be explicitly discouraged, but you'd only know that by reading the entire doc. Based on the number of real-world examples I've seen of people memoizing simple lookups, I don't think many people are getting all the way to the bottom.

Could the docs be updated so that the examples follow best-practices? (I'm happy to open a PR if that's helpful.) Thanks!!

evandavis commented 1 year ago

sorry, dupe of #598

markerikson commented 1 year ago

Yeah, the Reselect README could definitely use some love, especially with v5 upcoming.

aryaemami59 commented 12 months ago

Yeah, the Reselect README could definitely use some love, especially with v5 upcoming.

I'm on it. It's certainly going to look different...