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-collect

v0.2.1

Published

Helpful function for connecting redux state and actions to React components.

Downloads

9

Readme

React Redux Collect

Build Status Coverage Status Dependency Status devDependency Status JavaScript Style Guide

This is the consequence of some feedback and meditation on react-redux-connect-helpers.
Chiefly, the resulting nested mess of connectors after composition:

screen shot 2017-04-08 at 3 01 55 pm
screenshot from React Developer Tools

This function implements the same core functionality as react-redux-connect-helper's connectStateValue, but by building up mapStateToProps and mapDispatchToProps arguments to be passed to a single connect call.

Collect some values in state and connect them as props. :+1:

Installation

$ npm install react-redux-connect-helpers

Usage

import collect from 'react-redux-collect'

const provideProps = collect([<path>, <name>, <transformer>], ...)

The arguments to connect a value in state are ordered as follows:

  • path: An array or string denoting the key(s) to access the value in state.
  • name (optional): The key of the resulting prop, defaults to. the last key in path.
  • transformer (optional): Function to transform the accessed value or action.
    For values, will pass the value as the first param and state as the second.
    For actions, will pass the action as the first param and props as the second.

For example, where state and actionCreators look like this:

const state = {
  id: 'SST-130',
  title: 'You\'re Living All Over Me',
  artist: 'Dinosaur Jr.',
  tracks: [
    { title: 'Little Fury Things', length: '3:06' },
    { title: 'Kracked', length: '2:50' },
    { title: 'Sludgefeast', length: '5:17' },
    { title: 'The Lung', length: '3:51' },
    { title: 'Raisans', length: '3:50' },
    { title: 'Tarpit', length: '4:36' },
    { title: 'In a Jar', length: '3:28' },
    { title: 'Lose', length: '3:11' },
    { title: 'Poledo', length: '5:43' }
  ]
}

const actionCreators = {
  openPlayer: () => ({
    type: 'OPEN_PLAYER'
  }),
  playAlbum: albumId => ({
    type: 'PLAY_ALBUM',
    payload: albumId
  })
}

This code:

import { createCollect } from 'react-redux-collect'
import actionCreators from './actionCreators'

const collect = createCollect(actionCreators)

const provideProps = collect(
  'id',
  ['title']
  ['artist', 'band'],
  [['tracks', 0], 'firstTrack'],
  ['tracks', 'numTracks', tracks => tracks.length],
  [['actions', 'openPlayer']],
  [['actions', 'playAlbum'], 'onClick', (action, {id}) => action.bind(null, id)]
)

Is equivalent to:

import { connect } from 'react-redux'
import actionCreators from './actionCreators'

const mapStateToProps = (state, ownProps) => ({
  title: state.title,
  band: state.artist,
  firstTrack: state.tracks[0],
  numTracks: state.tracks.length
})

const mapDispatchToProps = (dispatch, ownProps) => ({
  openPlayer: (...args) =>
    dispatch(actionCreators.openPlayer(...args)),
  onClick: (...args) =>
    dispatch(actionCreators.playAlbum(props.id, ...args))
})

const provideProps = connect(
  mapStateToProps,
  mapDispatchToProps
)

Immutable

If your state uses Immutable.js, import with react-redux-collect/immutable.

Tricks

Creating collect is only necessary if you intend to connect actions as props.
Otherwise you can just use the default export.

import collect from 'react-redux-collect'

// Ideally a created collect is declared in another file
// and imported as needed throughout your application.

You can pass selector functions instead of path arrays.

import collect from 'react-redux-collect'

const selectFirstTrack = state => state.tracks[0]

// props: { firstTrack: { title: 'Little Fury Things', length: '3:06' } }
const provideProps = collect([selectFirstTrack, 'firstTrack'])

You can spread a state value into props using the string '...'.

import collect from 'react-redux-collect'

// props: { title: 'Kracked', length: '2:50' }
const provideProps = collect([['tracks', 1], '...'])

You can pass the entirety of state into props using the string '*'.

import collect from 'react-redux-collect'

// props: {...state}
const provideProps = collect('*')
// NOTE:
// If state['*'] is not undefined, collect will throw an error