Skip to content

Selectors with dynamic dependencies and params

License

Notifications You must be signed in to change notification settings

spautz/dynamic-selectors

Repository files navigation

Dynamic Selectors

Selectors with parameters and dynamic dependencies.

See Selector Comparison for a walkthrough.

build status test coverage

Packages

npm version test coverage dependencies status gzip size

Core functionality for dynamic selectors, independent of any other library.
This is the main package.

npm version test coverage dependencies status gzip size

Call Reselect selectors from within dynamic selectors, and use dynamic selectors as dependencies for Reselect selectors.

What is this?

Selectors are memoized functions that transform data -- like computing a derived value from state -- which only re-run when their dependencies change. Reselect is a popular selector library: Redux's Deriving Data with Selectors doc is a good introduction to them.

Dynamic selectors are built like plain functions. There is no up-front registration to connect selectors together, you can pass arguments to them, and they can call each other just like regular functions.

This may be used with a state library like Redux, or on its own as a general memoization util.

Features

Pass arguments to selector functions
Results are memoized by the params you pass to a selector, so selectBooks({ authorId: 3 }) and selectBooks({ authorId: 4 }) will work properly and be cached independently.
Call selectors from within selectors
A selector can call other selectors from if blocks, loops, or any other controls -- or even recursively.
Auto-detected dependencies
When a selector runs, any secondary selectors it calls get marked as dependencies. It won't re-run unless those dependencies return something new. The dependencies can change from one run to the next.
Equality comparisons / caching strategy
Like the useSelector hook, you can specify your own comparison function to 'freeze' updates. This may be customized for each selector.

Comparison between Reselect and Dyanamic Selectors