+0
日增长
18,999
stars
668
forks
158
watchers
369
prs
424
issues
49
releases
README
Reselect
[![npm package][npm-badge]][npm][![Coveralls][coveralls-badge]][coveralls][![GitHub Workflow Status][build-badge]][build]![TypeScript][typescript-badge]
A library for creating memoized "selector" functions. Commonly used with Redux, but usable with any plain JS immutable data as well.
- Selectors can compute derived data, allowing [Redux] to store the minimal possible state.
- Selectors are efficient. A selector is not recomputed unless one of its arguments changes.
- Selectors are composable. They can be used as input to other selectors.
The Redux docs usage page on Deriving Data with Selectors covers the purpose and motivation for selectors, why memoized selectors are useful, typical Reselect usage patterns, and using selectors with [React-Redux].
Installation
Redux Toolkit
While Reselect is not exclusive to [Redux], it is already included by default in the official Redux Toolkit package - no further installation needed.
ts
import { createSelector } from '@reduxjs/toolkit'
Standalone
For standalone usage, install the reselect package:
bash
NPM
npm install reselect
Yarn
yarn add reselect
pnpm
pnpm add reselect
---
Documentation
The Reselect docs are available at https://reselect.js.org, and include usage guides and API references:
- Introduction
- How Does Reselect Work?
- API Reference:
- [createSelector]
- [createSelectorCreator]
- [createStructuredSelector]
- Development-Only Checks
- [lruMemoize]
- [weakMapMemoize]
- FAQ
Basic Usage
Reselect exports a [createSelector] API, which generates memoized selector functions. [createSelector] accepts one or more [input selectors], which extract values from arguments, and a [result function] function that receives the extracted values and should return a derived value. If the generated [output selector] is called multiple times, the output will only be recalculated when the extracted values have changed.
You can play around with the following example in this CodeSandbox:
ts
import { createSelector } from 'reselect'
interface RootState {
todos: { id: number; completed: boolean }[]
alerts: { id: number; read: boolean }[]
}
const state: RootState = {
todos: [
{ id: 0, completed: false },
{ id: 1, completed: true }
],
alerts: [
{ id: 0, read: false },
{ id: 1, read: true }
]
}
const selectCompletedTodos = (state: RootState) = {
console.log('selector ran')
return state.todos.filter(todo = todo.completed === true)
}
selectCompletedTodos(state) // selector ran
selectCompletedTodos(state) // selector ran
selectCompletedTodos(state) // selector ran
const memoizedSelectCompletedTodos = createSelector(
[(state: RootState) = state.todos],
todos = {
console.log('memoized selector ran')
return todos.filter(todo = todo.completed === true)
}
)
memoizedSelectCompletedTodos(state) // memoized selector ran
memoizedSelectCompletedTodos(state)
memoizedSelectCompletedTodos(state)
console.log(selectCompletedTodos(state) Top issues
#559Inlining wrappers over `createSelector` causes selector args to be of `any` type12 comments · 2021-11-30#376Regression when using lodash for an unbounded cache and reselect 4.012 comments · 2018-10-04#691Question: Why can't we support `createAsyncSelector`?11 comments · 2024-01-26#670Better call stack for selector warnings11 comments · 2023-12-18#571`createSelector` can't handle generic state6 comments · 2022-03-30#544Confusing behavior of caching mechanism in 4.1.2 for parametrized selectors6 comments · 2021-11-08#666Type loss in `createSelector` with inline function declarations passed as separate arguments4 comments · 2023-12-17#728Problem of not getting result value when using currySelector by mapping another currySelector2 comments · 2024-07-17
Discussions
#827External selector traces for cache hits and equal-result reuse0 comments · 2026-10-25#787v5.3.00 comments · 2026-08-22#746Memoizing rapidly changing state1 comments · 2025-04-10#741What's the correct way to return an empty array as fallback from a selector?1 comments · 2025-03-07#711What is the suggested way to migrate over to v5?1 comments · 2024-03-20#662What's the correct way of conditionally returning an input?1 comments · 2023-12-16#658v5.0.00 comments · 2023-12-04#617Is it safe to use createSelector in SSR?2 comments · 2023-06-13
Latest release
v5.3.0 v5.3.0 2026-08-22This feature release adds a maxSize option to weakMapMemoize to bound cache growth, adds a new development-mode check that warns when a selector's cache grows without bound, improves memoization performance, removes the experimental unstable_autotrackMemoize, and updates our TypeScript support matrix and documentation. Changelog maxSize Option for weakMapMemoize weakMapMemoize has been the default memoizer since v5.0, and its main benefit is the infinite cache size. Prior to v5, selector instances had a default cache size of 1, which meant having to create unique selector instances