Skip to content

Commit 38de0f0

Browse files
committed
Update docs on Balanced Selector Usage
1 parent 61647ff commit 38de0f0

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

docs/usage/deriving-data-selectors.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ That way, if we happen to make an update to the structure of the todos slice sta
576576

577577
It's possible to add _too many_ selectors to an application. **Adding a separate selector function for every single field is not a good idea!** That ends up turning Redux into something resembling a Java class with getter/setter functions for every field. It's not going to _improve_ the code, and it's probably going to make the code _worse_ - maintaining all those extra selectors is a lot of additional effort, and it will be harder to trace what values are being used where.
578578

579-
Similarly, **don't make every single selector memoized!**. Memoization is only needed if you are truly _deriving_ results, _and_ if the derived results would likely create new references every time. **A selector function that does a direct lookup and return of a value should be a plain function, not memoized**.
579+
Similarly, **don't make every single selector memoized!**. Memoization is only needed if the selector returns a new reference every time it runs, or if the calculation logic it executes is expensive. **A selector function that does a direct lookup and return of a value should be a plain function, not memoized**.
580580

581581
Some examples of when and when not to memoize:
582582

@@ -586,7 +586,9 @@ const selectTodos = state => state.todos
586586
const selectNestedValue = state => state.some.deeply.nested.field
587587
const selectTodoById = (state, todoId) => state.todos[todoId]
588588

589-
// ❌ DO NOT memoize: deriving data, but will return a consistent result
589+
// 🤔 MAYBE memoize: deriving data, but will return a consistent result.
590+
// Memoization might be useful if the selector is used in many places
591+
// or the list being iterated over is long.
590592
const selectItemsTotal = state => {
591593
return state.items.reduce((result, item) => {
592594
return result + item.total

0 commit comments

Comments
 (0)