react-code-dataset / reselect /website /docs /api /createSelectorCreator.mdx
Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
raw
history blame
8.2 kB
---
id: createSelectorCreator
title: createSelectorCreator
sidebar_label: createSelectorCreator
hide_title: true
description: 'createSelectorCreator'
---
import { InternalLinks } from '@site/src/components/InternalLinks'
# `createSelectorCreator`
Accepts either a `memoize` function and `...memoizeOptions` rest parameter, or since 5.0.0 an `options` object containing a `memoize` function and creates a custom selector creator function.
## Parameters (since 5.0.0)
| Name | Description |
| :----------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `options` | An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside <InternalLinks.CreateSelector /> (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional. |
| `options.argsMemoize?` | The optional memoize function that is used to memoize the arguments passed into the <InternalLinks.OutputSelector /> generated by <InternalLinks.CreateSelector /> (e.g., `lruMemoize` or `weakMapMemoize`). <br /> **`Default`** = `lruMemoize` before 5.0.0 and `weakMapMemoize` after |
| `options.argsMemoizeOptions?` | Optional configuration options for the `argsMemoize` function. These options are passed to the `argsMemoize` function as the second argument. <br /> since 5.0.0 |
| `options.inputStabilityCheck?` | Overrides the global input stability check for the selector. Possible values are: <br /> `once` - Run only the first time the selector is called. <br /> `always` - Run every time the selector is called. <br /> `never` - Never run the input stability check. <br /> **`Default`** = `'once'` <br /> since 5.0.0 |
| `options.memoize` | The memoize function that is used to memoize the `resultFunc` inside <InternalLinks.CreateSelector /> (e.g., `lruMemoize` or `weakMapMemoize`). since 5.0.0 |
| `options.memoizeOptions?` | Optional configuration options for the `memoize` function. These options are passed to the `memoize` function as the second argument. <br /> since 5.0.0 |
## Parameters
| Name | Description |
| :-------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------- |
| `memoize` | The `memoize` function responsible for memoizing the `resultFunc` inside <InternalLinks.CreateSelector /> (e.g., `lruMemoize` or `weakMapMemoize`). |
| `...memoizeOptionsFromArgs` | Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards. |
## Returns
A customized <InternalLinks.CreateSelector /> function.
## Type Parameters
| Name | Description |
| :-------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `MemoizeFunction` | The type of the memoize function that is used to memoize the `resultFunc` inside <InternalLinks.CreateSelector /> (e.g., `lruMemoize` or `weakMapMemoize`). |
| `ArgsMemoizeFunction` | The type of the optional memoize function that is used to memoize the arguments passed into the <InternalLinks.OutputSelector /> generated by <InternalLinks.CreateSelector /> (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `lruMemoize` will be used. |
## Examples
### Using `options` (since 5.0.0)
```ts
const customCreateSelector = createSelectorCreator({
memoize: customMemoize, // Function to be used to memoize `resultFunc`
memoizeOptions: [memoizeOption1, memoizeOption2], // Options passed to `customMemoize` as the second argument onwards
argsMemoize: customArgsMemoize, // Function to be used to memoize the selector's arguments
argsMemoizeOptions: [argsMemoizeOption1, argsMemoizeOption2] // Options passed to `customArgsMemoize` as the second argument onwards
})
const customSelector = customCreateSelector(
[inputSelector1, inputSelector2],
resultFunc // `resultFunc` will be passed as the first argument to `customMemoize`
)
customSelector(
...selectorArgs // Will be memoized by `customArgsMemoize`
)
```
### Using `memoize` and `...memoizeOptions`
`createSelectorCreator` can be used to make a customized version of <InternalLinks.CreateSelector />.
The `memoize` argument is a memoization function to replace the default configured memoizer (normally `weakMapMemoize`).
The `...memoizeOptions` rest parameters are zero or more configuration options to be passed to `memoizeFunc`. The selectors `resultFunc` is passed as the first argument to `memoize` and the `memoizeOptions` are passed as the second argument onwards:
```ts
const customSelectorCreator = createSelectorCreator(
customMemoize, // Function to be used to memoize `resultFunc`
option1, // `option1` will be passed as second argument to `customMemoize`
option2, // `option2` will be passed as third argument to `customMemoize`
option3 // `option3` will be passed as fourth argument to `customMemoize`
)
const customSelector = customSelectorCreator(
[inputSelector1, inputSelector2],
resultFunc // `resultFunc` will be passed as first argument to `customMemoize`
)
```
Internally `customSelector` calls the memoize function as follows:
```ts
customMemoize(resultFunc, option1, option2, option3)
```
### Additional Examples
#### Customize `equalityCheck` for `lruMemoize`
```js
import { createSelectorCreator, lruMemoize } from 'reselect'
import isEqual from 'lodash.isequal'
// create a "selector creator" that uses lodash.isequal instead of ===
const createDeepEqualSelector = createSelectorCreator(lruMemoize, isEqual)
// use the new "selector creator" to create a selector
const selectSum = createDeepEqualSelector(
[state => state.values.filter(val => val < 5)],
values => values.reduce((acc, val) => acc + val, 0)
)
```
#### Use memoize function from Lodash for an unbounded cache
```js
import { createSelectorCreator } from 'reselect'
import memoize from 'lodash.memoize'
const hashFn = (...args) =>
args.reduce((acc, val) => acc + '-' + JSON.stringify(val), '')
const customSelectorCreator = createSelectorCreator(memoize, hashFn)
const selector = customSelectorCreator(
[state => state.a, state => state.b],
(a, b) => a + b
)
```