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

react-redux-connect-keyed-ownprops

v1.0.3

Published

react-redux connect with factories, safely keyed by given ownProps.

Downloads

9

Readme

react-redux-connect-keyed-ownprops

react-redux connect with factories, safely keyed by given ownProps.

npm Travis CI Codecov

The Problem

When connecting a React component to Redux, you may need to refer to ownProps. For optimal performance, the react-redux documentation recommends using factory functions, so that your component does not have to re-execute mapStateTo* functions on every prop change.

Some reach for a library like re-reselect which does selector routing based on dynamic arguments from ownProps, but this misses the optimzation of the factory function.

But using the factory functions means your component instance will not respond to changes in values consumed from ownProps. One workaround for this is to use key props at consumption-site. But then consumers have to know which props your makeMapStateTo* rely on internally.

The Solution

This library encapsulates this by offering a higher-order component (HOC) connectOwnProps, which behaves almost identically to connect from react-redux, but with an additional first argument specifying ownProps needed in mapStateTo* or makeMapStateTo* functions, and that it manages an internal key on the connected component, which it keeps up to date while monitoring the specified ownProps for changes (shallow compare), and when it updates, triggers a new component instance to be created, which re-executes its internal makeMapStateTo* factory functions.

It also provides the managed subset of ownProps as a second argument to your mapStateTo* or makeMapStateTo* functions, instead of the full set, which encourages safe usage by disallowing access to initial values decoupled from re-instancing.

Example

See a live demo here (and source).

Here's some sample code:

import connectOwnProps from 'react-redux-connect-keyed-ownprops'

// ...

const makeMapStateToProps = (state, pickedOwnProps) => {
  // Now `pickedOwnProps` has only the props requested below.
  const selectMyProp = makeSelectMyProp(pickedOwnProps.myProp)

  return state => ({
    myProp: selectMyProp(state)
  })
}

export default connectOwnProps(
  ['myProp', 'myProp2'], // Specify to pluck these props from `ownProps`.
  makeMapStateToProps
)(MyComponent)

Pick Props

You can specify which ownProps to pick by using an array of keys, e.g.:

export default connectOwnProps(['myProp', 'myProp2'], makeMapStateToProps)(
  MyComponent
)

Or just a single key:

export default connectOwnProps('myProp', makeMapStateToProps)(MyComponent)

Or a custom function to select from ownProps (or derive relevant values). The output of this function is what will be shallow-compared for changes to manage the internal key.

export default connectOwnProps(
  ownProps => ({
    myProp: ownProps.myProp
  }),
  makeMapStateToProps
)(MyComponent)