Gathering detailed insights and metrics for reselect
Gathering detailed insights and metrics for reselect
Gathering detailed insights and metrics for reselect
Gathering detailed insights and metrics for reselect
npm install reselect
Typescript
Module System
Node Version
NPM Version
99.5
Supply Chain
99.6
Quality
84.8
Maintenance
100
Vulnerability
100
License
19,043 Stars
1,262 Commits
673 Forks
164 Watching
11 Branches
102 Contributors
Updated on 03 Dec 2024
Minified
Minified + Gzipped
TypeScript (79.92%)
MDX (18.68%)
CSS (1.14%)
JavaScript (0.26%)
Cumulative downloads
Total Downloads
Last day
-3.5%
1,495,970
Compared to previous day
Last week
-13.3%
7,448,282
Compared to previous week
Last month
3.2%
33,897,557
Compared to previous month
Last year
16%
381,592,039
Compared to previous year
28
A library for creating memoized "selector" functions. Commonly used with Redux, but usable with any plain JS immutable data as well.
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.
While Reselect is not exclusive to Redux, it is already included by default in the official Redux Toolkit package - no further installation needed.
1import { createSelector } from '@reduxjs/toolkit'
For standalone usage, install the reselect
package:
1# NPM 2npm install reselect 3 4# Yarn 5yarn add reselect
The Reselect docs are available at https://reselect.js.org, and include usage guides and API references:
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:
1import { createSelector } from 'reselect' 2 3interface RootState { 4 todos: { id: number; completed: boolean }[] 5 alerts: { id: number; read: boolean }[] 6} 7 8const state: RootState = { 9 todos: [ 10 { id: 0, completed: false }, 11 { id: 1, completed: true } 12 ], 13 alerts: [ 14 { id: 0, read: false }, 15 { id: 1, read: true } 16 ] 17} 18 19const selectCompletedTodos = (state: RootState) => { 20 console.log('selector ran') 21 return state.todos.filter(todo => todo.completed === true) 22} 23 24selectCompletedTodos(state) // selector ran 25selectCompletedTodos(state) // selector ran 26selectCompletedTodos(state) // selector ran 27 28const memoizedSelectCompletedTodos = createSelector( 29 [(state: RootState) => state.todos], 30 todos => { 31 console.log('memoized selector ran') 32 return todos.filter(todo => todo.completed === true) 33 } 34) 35 36memoizedSelectCompletedTodos(state) // memoized selector ran 37memoizedSelectCompletedTodos(state) 38memoizedSelectCompletedTodos(state) 39 40console.log(selectCompletedTodos(state) === selectCompletedTodos(state)) //=> false 41 42console.log( 43 memoizedSelectCompletedTodos(state) === memoizedSelectCompletedTodos(state) 44) //=> true
As you can see from the example above, memoizedSelectCompletedTodos
does not run the second or third time, but we still get the same return value as last time.
In addition to skipping unnecessary recalculations, memoizedSelectCompletedTodos
returns the existing result reference if there is no recalculation. This is important for libraries like React-Redux or React that often rely on reference equality checks to optimize UI updates.
createSelector
, and are called with all selector arguments. They are responsible for extracting and providing necessary values to the result function.createSelector
.Dependencies
: Same as input selectors. They are what the output selector "depends" on.The below example serves as a visual aid:
1const outputSelector = createSelector(
2 [inputSelector1, inputSelector2, inputSelector3], // synonymous with `dependencies`.
3 resultFunc // Result function
4)
Version 5.0.0 introduces several new features and improvements:
Customization Enhancements:
createSelectorCreator
, allowing for customized memoize
and argsMemoize
functions, alongside their respective options (memoizeOptions
and argsMemoizeOptions
).createSelector
function now supports direct customization of memoize
and argsMemoize
within its options object.Memoization Functions:
weakMapMemoize
and unstable_autotrackMemoize
.memoize
and argsMemoize
into the output selector fields for debugging purposes.TypeScript Support and Performance:
Type instantiation is excessively deep and possibly infinite
error.Selector API Enhancements:
createStructuredSelector
due to its susceptibility to runtime errors.Additional Functionalities:
dependencyRecomputations
and resetDependencyRecomputations
to the output selector fields. These additions provide greater control and insight over input selectors, complementing the new argsMemoize
API.inputStabilityCheck
, a development tool that runs the input selectors twice using the same arguments and triggers a warning If they return differing results for the same call.identityFunctionCheck
, a development tool that checks to see if the result function returns its own input.These updates aim to enhance flexibility, performance, and developer experience. For detailed usage and examples, refer to the updated documentation sections for each feature.
Breaking Changes:
ParametricSelector
and OutputParametricSelector
types. Their functionalities are now integrated into Selector
and OutputSelector
respectively, which inherently support additional parameters.MIT
Originally inspired by getters in NuclearJS, subscriptions in re-frame and this proposal from speedskater.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
Found 5/6 approved changesets -- score normalized to 8
Reason
7 existing vulnerabilities detected
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More