npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-bind-selectors

v1.1.13

Published

A Redux store enhancer for computing derived state by binding selectors to the store

Downloads

745

Readme

npm test

redux-bind-selectors

A Redux store enhancer for computing derived state by binding selectors to a Redux store, so that getState() incorporates derived data.

import { createStore } from 'redux'
import bindSelectors from 'redux-bind-selectors'

const store = createStore(
  myReducer, // The Redux reducer
  { numbers: [4, 6, 9, 2] }, // Initial state (optional)
  bindSelectors({ total }) // Bind the `total` selector
)

store.getState()
// {
//   numbers: [4, 6, 9, 2], // <-- initial state from the store
//   total: 21              // <-- result of the `total` selector
// }

Installation

npm install --save redux-bind-selectors

Usage

bindSelectors() takes an object where the keys are paths in the state object, and the properties are selector functions. This is analogous to the Redux combineSelectors() function.

const enhancer = bindSelectors({
  selector1: myReselectSelector,
  selector2: state => state.a + state.b
})

Selectors are pure functions that take the state object as their only argument. Check out reselect to build efficient selectors.

When creating the store, the enhancer should be the last argument to the createStore() function. If you have more than one enhancer, you can use the compose() function in Redux to compose them.

const store1 = createStore(reducer, enhancer)
const store2 = createStore(reducer, initialState, enhancer)

Motivation

A Redux store should contain the minimum representation of state, so rather than storing:

{
  numbers: [4, 6, 9, 2],
  total: 21 // <-- this can be calculated from `numbers`
}

it should just store:

{
  numbers: [4, 6, 9, 2]
}

and calculate total when needed with a selector:

const total = state => state.numbers.reduce((sum, value) => sum + value, 0)

(Pro tip: reselect would be more efficient)

But...

Instead of a single, simple state object, state is now split between the store and a collection of selector functions, which adds complexity.

Solution without redux-bind-selectors

Use mapStateToProps() in React Redux, to connect the selectors to the state.

Solution with redux-bind-selectors

Use this module to bind the selectors to the store, so that the store runs the selectors for you, producing a single object. This approach retains the advantages of minimum state representation, and separation of concerns between reducers and selectors.

Which one should I use?

If you are new to React and Redux, then you should initially consider mapStateToProps(), as recommended by the Redux documentation.

You should try out this module if you are not using React, you prefer to keep view and model logic separate, you live on the edge, or you consider this approach to be more elegant. It's relatively easy to switch between the two, or do both at the same time.

If the output of getState() is used for other purposes (for instance, to persist the state), then careful consideration should be given as to how this module will affect that.

Notes

  • A selector cannot have the same path in the state object as a reducer. (This is why we do not simply reuse the createStructuredSelector() function from reselect)
  • Paths are all top level object keys
  • A selector cannot return undefined, in order to be consistent with Redux where a combined reducer cannot return undefined. Instead, use null.

License

See LICENSE.md

Contributing

See CONTRIBUTING.md

JavaScript Style Guide