Skip to content

Commit 6e3c35b

Browse files
authored
Merge pull request #4804 from kopeczech/patch-3
Replace Reselect example with a more realistic one
2 parents 44486e9 + eccc5a2 commit 6e3c35b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

docs/usage/deriving-data-selectors.md

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,36 +228,31 @@ const brokenSelector = createSelector(
228228
Similarly, a memoized selector should _never_ use `state => state` as an input! That will force the selector to always recalculate.
229229
:::
230230

231-
In typical Reselect usage, you write your top-level "input selectors" as plain functions, and use `createSelector` to create memoized selectors that calculate derived values:
231+
In typical Reselect usage, you write your top-level "input selectors" as simple functions that just return values nested somewhere inside the state object. Then, you use `createSelector` to create memoized selectors that take one or more of these values as input and produce new derived values:
232232

233233
```js
234-
const state = {
235-
a: {
236-
first: 5
237-
},
238-
b: 10
239-
}
240-
241-
const selectA1 = state => state.a.first
242-
const selectB = state => state.b
243-
244-
const selectResult = createSelector([selectA1, selectB], (a1, b) => {
245-
console.log('Output selector running')
246-
return a1 + b
247-
})
234+
const selectTodos = state => state.todos.items
235+
const selectCurrentUser = state => state.users.currentUser
236+
237+
const selectTodosForCurrentUser = createSelector(
238+
[selectTodos, selectCurrentUser],
239+
(todos, currentUser) => {
240+
console.log('Output selector running')
241+
return todos.filter(todo => todo.ownerId === currentUser.userId)
242+
}
243+
)
248244

249-
const result = selectResult(state)
245+
const todosForCurrentUser1 = selectTodosForCurrentUser(state)
250246
// Log: "Output selector running"
251-
console.log(result)
252-
// 15
253247

254-
const secondResult = selectResult(state)
248+
const todosForCurrentUser2 = selectTodosForCurrentUser(state)
255249
// No log output
256-
console.log(secondResult)
257-
// 15
250+
251+
console.log(todosForCurrentUser1 === todosForCurrentUser2)
252+
// true
258253
```
259254

260-
Note that the second time we called `selectResult`, the "output selector" didn't execute. Because the results of `selectA1` and `selectB` were the same as the first call, `selectResult` was able to return the memoized result from the first call.
255+
Note that the second time we called `selectTodosForCurrentUser`, the "output selector" didn't execute. Because the results of `selectTodos` and `selectCurrentUser` were the same as the first call, `selectTodosForCurrentUser` was able to return the memoized result from the first call.
261256

262257
### `createSelector` Behavior
263258

0 commit comments

Comments
 (0)