Choose a different memoizer
You need selector result memoization behavior different from the default configured memoizer.
Redux Toolkit · Function
Creates a customized createSelector factory by choosing the memoization function used for selector result functions. It accepts either a memoizer with optional configuration arguments or an options object.
createSelectorCreator builds a customized createSelector function by configuring the memoizer used for each selector's result function. Pass a memoization function directly, optionally followed by its configuration values, or pass an options object with memoize and related settings. The returned creator accepts normal createSelector inputs and returns memoized selectors.
createSelectorCreator(memoize, ...memoizeOptions)
createSelectorCreator(options)| Name | Type | Description |
|---|---|---|
memoize | MemoizeFunction | Memoization function used for the result function, such as lruMemoize or weakMapMemoize; this is also supplied through the options-object form. |
...memoizeOptions | unknown[] | Optional configuration values forwarded to the memoization function after the result function. |
options | CreateSelectorOptions | Object form introduced in Reselect 5.0.0; it contains memoize and optional memoization configuration. |
A customized createSelector function.
createSelector export.weakMapMemoize; choose a replacement deliberately.import { createSelectorCreator, lruMemoize } from '@reduxjs/toolkit'
type State = { todos: { id: number; done: boolean }[] }
const createAppSelector = createSelectorCreator(lruMemoize)
const selectCompletedCount = createAppSelector(
[(state: State) => state.todos],
(todos) => todos.filter((todo) => todo.done).length,
)
const state: State = { todos: [{ id: 1, done: true }, { id: 2, done: false }] }
console.log(selectCompletedCount(state))Creates a selector factory using LRU memoization, then uses it to derive the number of completed todos.
You need selector result memoization behavior different from the default configured memoizer.
A memoizer exposes tuning values such as cache-related settings.
An RTK Query API should use a custom selector creator across its modules.
createSelectorCreator do?It creates a customized version of createSelector. The customization primarily replaces the memoization function used for the selector result function, and the returned value is itself a selector creator.
Follow-up: How can it receive memoizer configuration? Use the rest parameters after the memoizer, or provide an options object containing memoize and additional settings.
The first argument is the memoization function responsible for memoizing the resultFunc. Common choices documented for this API include lruMemoize and weakMapMemoize.
Follow-up: What happens when no memoizer is explicitly provided in the relevant memoization configuration? The documentation says weakMapMemoize is used.
The creator supports a positional form with memoize followed by ...memoizeOptions, and an options-object form containing memoize. The object form was added in version 5.0.0.
Follow-up: Why use a custom creator in RTK Query? Supply the resulting selector creator through the API-building modules when custom selector memoization is needed.
createSelectorcreateDraftSafeSelectorCreatorlruMemoizeweakMapMemoize