About this project
Reselect is a library for creating memoized "selector" functions. It is commonly used with Redux, but it can also be used with any plain JavaScript immutable data.
Key characteristics described in the README:
- 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 project points to the Redux documentation page on deriving data with selectors for background on purpose, motivation, and typical usage patterns, including use with React-Redux.
Installation options:
- Redux Toolkit: Reselect is already included by default in the official Redux Toolkit package, so no further installation is needed. It can be imported as `createSelector` from `@reduxjs/toolkit`.
- Standalone: install the `reselect` package via npm, Yarn, or pnpm.
Documentation is hosted at reselect.js.org and includes an introduction, an explanation of how Reselect works, API references for `createSelector`, `createSelectorCreator`, `createStructuredSelector`, development-only checks, `lruMemoize`, and `weakMapMemoize`, plus an FAQ.
Basic usage: `createSelector` accepts one or more input selectors that extract values from arguments, and a result function that receives the extracted values and returns a derived value. The generated output selector recalculates only when the extracted values change. The README includes an example showing that a plain selector runs on every call, while a memoized selector skips recalculation and returns the existing result reference when inputs are unchanged. This reference stability is noted as important for libraries like React-Redux and React that rely on reference equality checks to optimize UI updates.
The README defines terminology: selector function, input selectors, output selector, result function, and dependencies.
Version 5.0.0 changes described include:
- Customization: options object for `createSelectorCreator` with customized `memoize` and `argsMemoize` functions and their options; `createSelector` supports direct customization of `memoize` and `argsMemoize`.
- Memoization: new default memoization function `weakMapMemoize`; `memoize` and `argsMemoize` exposed on output selector fields for debugging.
- TypeScript: support for TypeScript versions below 4.7 discontinued; improved TypeScript performance for nesting output selectors, with the nesting limit increased from about 8 to around 30, reducing deep type instantiation errors.
- API: removed the second overload of `createStructuredSelector` due to susceptibility to runtime errors.
- Additional features: `dependencyRecomputations` and `resetDependencyRecomputations` on output selector fields; `inputStabilityCheck` development tool that runs input selectors twice with the same arguments and warns if results differ; `identityFunctionCheck` development tool that checks whether the result function returns its own input.
- Breaking changes: removed `ParametricSelector` and `OutputParametricSelector` types, with their functionality integrated into `Selector` and `OutputSelector`.
The project is licensed under MIT. It was originally inspired by getters in NuclearJS, subscriptions in re-frame, and a Redux proposal.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.