File size: 13,171 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
---
id: createSelector
title: createSelector
sidebar_label: createSelector
hide_title: true
description: 'createSelector'
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import { InternalLinks } from '@site/src/components/InternalLinks'
# `createSelector`
Accepts one or more "<InternalLinks.InputSelectors />" (either as separate arguments or a single array),
a single "<InternalLinks.ResultFunction />", and an optional options object, and
generates a memoized selector function.
The **Redux docs usage page on [Deriving Data with Selectors](https://redux.js.org/usage/deriving-data-selectors)** covers the purpose and motivation for selectors, why memoized selectors are useful, and typical Reselect usage patterns.
```ts no-emit
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 <InternalLinks.InputSelectors />, can also be passed as separate arguments. |
| `resultFunc` | A function that takes the results of the <InternalLinks.InputSelectors /> as separate arguments. |
| `createSelectorOptions?` | An optional options object that allows for further customization per selector. |
## Returns
A memoized <InternalLinks.OutputSelector />.
### Output Selector Fields
The output selectors created by `createSelector` have several additional properties attached to them:
| Name | Description |
| ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `resultFunc` | The final function passed to <InternalLinks.CreateSelector />. |
| `memoizedResultFunc` | The memoized version of `resultFunc`. |
| `lastResult` | Returns the last result calculated by `memoizedResultFunc`. |
| `dependencies` | The array of the input selectors used by <InternalLinks.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 <InternalLinks.InputSelectors /> (<InternalLinks.Dependencies text={<code>dependencies</code>} />) have been recalculated. This is distinct from `recomputations`, which tracks the recalculations of the <InternalLinks.ResultFunction />. |
| `resetDependencyRecomputations` | Resets the `dependencyRecomputations` count to 0. |
| `memoize` | Function used to memoize the `resultFunc`. |
| `argsMemoize` | Function used to memoize the arguments passed into the <InternalLinks.OutputSelector />. |
## Type Parameters
| Name | Description |
| :---------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `InputSelectors` | The type of the <InternalLinks.InputSelectors /> array. |
| `Result` | The return type of the <InternalLinks.ResultFunction /> as well as the <InternalLinks.OutputSelector />. |
| `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 <InternalLinks.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 <InternalLinks.CreateSelectorCreator />. |
## Defining a Pre-Typed <InternalLinks.CreateSelector>`createSelector`</InternalLinks.CreateSelector>
As of Reselect 5.1.0, you can create a "pre-typed" version of <InternalLinks.CreateSelector /> where the `state` type is predefined. This allows you to set the `state` type once, eliminating the need to specify it with every <InternalLinks.CreateSelector /> call.
To do this, you can call `createSelector.withTypes<StateType>()`:
{/* START: createSelector/withTypes.ts */}
<Tabs
groupId='language'
defaultValue='ts'
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'},
]}>
<TabItem value='ts'>
```ts title="createSelector/withTypes.ts"
import { createSelector } from 'reselect'
export interface RootState {
todos: { id: number; completed: boolean }[]
alerts: { id: number; read: boolean }[]
}
export const createAppSelector = createSelector.withTypes<RootState>()
const selectTodoIds = createAppSelector(
[
// Type of `state` is set to `RootState`, no need to manually set the type
// highlight-start
state => state.todos
// highlight-end
],
todos => todos.map(({ id }) => id)
)
```
</TabItem>
<TabItem value='js'>
```js title="createSelector/withTypes.js"
import { createSelector } from 'reselect'
export const createAppSelector = createSelector.withTypes()
const selectTodoIds = createAppSelector(
[
// Type of `state` is set to `RootState`, no need to manually set the type
// highlight-start
state => state.todos
// highlight-end
],
todos => todos.map(({ id }) => id)
)
```
</TabItem>
</Tabs>
{/* END: createSelector/withTypes.ts */}
Import and use the pre-typed `createAppSelector` instead of the original, and the type for state will be used automatically.
:::danger Known Limitations
Currently this approach only works if input selectors are provided as a single array.
If you pass the input selectors as separate inline arguments, the parameter types of the result function will not be inferred.
As a workaround you can either
1. Wrap your input selectors in a single array
2. You can annotate the parameter types of the result function:
{/* START: createSelector/annotateResultFunction.ts */}
<Tabs
groupId='language'
defaultValue='ts'
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'},
]}>
<TabItem value='ts'>
```ts title="createSelector/annotateResultFunction.ts"
import { createSelector } from 'reselect'
interface Todo {
id: number
completed: boolean
}
interface Alert {
id: number
read: boolean
}
export interface RootState {
todos: Todo[]
alerts: Alert[]
}
export const createAppSelector = createSelector.withTypes<RootState>()
const selectTodoIds = createAppSelector(
// Type of `state` is set to `RootState`, no need to manually set the type
state => state.todos,
// ❌ Known limitation: Parameter types are not inferred in this scenario
// so you will have to manually annotate them.
// highlight-start
(todos: Todo[]) => todos.map(({ id }) => id)
// highlight-end
)
```
</TabItem>
<TabItem value='js'>
```js title="createSelector/annotateResultFunction.js"
import { createSelector } from 'reselect'
export const createAppSelector = createSelector.withTypes()
const selectTodoIds = createAppSelector(
// Type of `state` is set to `RootState`, no need to manually set the type
state => state.todos,
// ❌ Known limitation: Parameter types are not inferred in this scenario
// so you will have to manually annotate them.
// highlight-start
todos => todos.map(({ id }) => id)
// highlight-end
)
```
</TabItem>
</Tabs>
{/* END: createSelector/annotateResultFunction.ts */}
:::
:::tip
You can also use this API with <InternalLinks.CreateSelectorCreator /> to create a pre-typed custom selector creator:
{/* START: createSelector/createAppSelector.ts */}
<Tabs
groupId='language'
defaultValue='ts'
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'},
]}>
<TabItem value='ts'>
```ts title="createSelector/createAppSelector.ts"
import microMemoize from 'micro-memoize'
import { shallowEqual } from 'react-redux'
import { createSelectorCreator, lruMemoize } from 'reselect'
export interface RootState {
todos: { id: number; completed: boolean }[]
alerts: { id: number; read: boolean }[]
}
export const createAppSelector = createSelectorCreator({
memoize: lruMemoize,
argsMemoize: microMemoize,
memoizeOptions: {
maxSize: 10,
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual
},
argsMemoizeOptions: {
isEqual: shallowEqual,
maxSize: 10
},
devModeChecks: {
identityFunctionCheck: 'never',
inputStabilityCheck: 'always'
}
}).withTypes<RootState>()
const selectReadAlerts = createAppSelector(
[
// Type of `state` is set to `RootState`, no need to manually set the type
// highlight-start
state => state.alerts
// highlight-end
],
alerts => alerts.filter(({ read }) => read)
)
```
</TabItem>
<TabItem value='js'>
```js title="createSelector/createAppSelector.js"
import microMemoize from 'micro-memoize'
import { shallowEqual } from 'react-redux'
import { createSelectorCreator, lruMemoize } from 'reselect'
export const createAppSelector = createSelectorCreator({
memoize: lruMemoize,
argsMemoize: microMemoize,
memoizeOptions: {
maxSize: 10,
equalityCheck: shallowEqual,
resultEqualityCheck: shallowEqual
},
argsMemoizeOptions: {
isEqual: shallowEqual,
maxSize: 10
},
devModeChecks: {
identityFunctionCheck: 'never',
inputStabilityCheck: 'always'
}
}).withTypes()
const selectReadAlerts = createAppSelector(
[
// Type of `state` is set to `RootState`, no need to manually set the type
// highlight-start
state => state.alerts
// highlight-end
],
alerts => alerts.filter(({ read }) => read)
)
```
</TabItem>
</Tabs>
{/* END: createSelector/createAppSelector.ts */}
:::
|