createSelector
Accepts one or more "input selectors" (either as separate arguments or a single array), a single "result function", and an optional options object, and generates a memoized selector function.
The Redux docs usage page on Deriving Data with Selectors covers the purpose and motivation for selectors, why memoized selectors are useful, and typical Reselect usage patterns.
const selectTodosByCategory = createSelector(
[
// Pass input selectors with typed arguments
(state: RootState) => state.todos,
(state: RootState, category: string) => category
],
// Extracted values are passed to the result function for recalculation
(todos, category) => {
return todos.filter(t => t.category === category)
}
)
Parameters
Name | Description |
---|---|
inputSelectors | An array of input selectors, can also be passed as separate arguments. |
resultFunc | A function that takes the results of the input selectors as separate arguments. |
createSelectorOptions? | An optional options object that allows for further customization per selector. |
Returns
A memoized output selector.
Output Selector Fields
The output selectors created by createSelector
have several additional properties attached to them:
Name | Description |
---|---|
resultFunc | The final function passed to createSelector . |
memoizedResultFunc | The memoized version of resultFunc . |
lastResult | Returns the last result calculated by memoizedResultFunc . |
dependencies | The array of the input selectors used by createSelector to compose resultFunc . |
recomputations | Counts the number of times memoizedResultFunc has been recalculated. |
resetRecomputations | Resets the count of recomputations count to 0. |
dependencyRecomputations | Counts the number of times the input selectors (dependencies ) have been recalculated. This is distinct from recomputations , which tracks the recalculations of the result function. |
resetDependencyRecomputations | Resets the dependencyRecomputations count to 0. |
memoize | Function used to memoize the resultFunc . |
argsMemoize | Function used to memoize the arguments passed into the output selector. |
Type Parameters
Name | Description |
---|---|
InputSelectors | The type of the input selectors array. |
Result | The return type of the result function as well as the output selector. |
OverrideMemoizeFunction | The type of the optional memoize function that could be passed into the options object to override the original memoize function that was initially passed into createSelectorCreator . |
OverrideArgsMemoizeFunction | The type of the optional argsMemoize function that could be passed into the options object to override the original argsMemoize function that was initially passed into createSelectorCreator . |