Skip to main content

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

NameDescription
inputSelectorsAn array of input selectors, can also be passed as separate arguments.
resultFuncA 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:

NameDescription
resultFuncThe final function passed to createSelector.
memoizedResultFuncThe memoized version of resultFunc.
lastResultReturns the last result calculated by memoizedResultFunc.
dependenciesThe array of the input selectors used by createSelector to compose resultFunc.
recomputationsCounts the number of times memoizedResultFunc has been recalculated.
resetRecomputationsResets the count of recomputations count to 0.
dependencyRecomputationsCounts the number of times the input selectors (dependencies) have been recalculated. This is distinct from recomputations, which tracks the recalculations of the result function.
resetDependencyRecomputationsResets the dependencyRecomputations count to 0.
memoizeFunction used to memoize the resultFunc.
argsMemoizeFunction used to memoize the arguments passed into the output selector.

Type Parameters

NameDescription
InputSelectorsThe type of the input selectors array.
ResultThe return type of the result function as well as the output selector.
OverrideMemoizeFunctionThe 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.
OverrideArgsMemoizeFunctionThe 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.