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-data-collections

v0.0.12

Published

Redux actions, reducers and selectors for managing data in JSONAPI format.

Downloads

9

Readme

redux-data-collections

Redux actions, reducers and selectors for managing data in JSONAPI format.

About

Installation

yarn add redux-data-collections

Usage

These examples presume you're using React Redux Starter Kit (because I do). There's nothing particularly special about one react-redux kit versus another. You should be able to adapt these examples to your needs.

Redux-data-collections exports createCollectionReducer by default. A collection reducer will only handle actions with a meta.type that matches the collection type. For instance, if you create a reducer like createCollectionReducer('cars'), the created reducer will only respond to actions where the action.meta.type === 'cars'. In practice, this is managed for you.

Simply add your collections to your root reducer in src/store/reducers.js

import { combineReducers } from 'redux'
import createCollectionReducer from 'redux-data-collections'

// in a naive world we'd just create the reducers and move on
export const createRootReducer = (asyncReducers) => {
  return combineReducers({
    articles: createCollectionReducer('article'),
    comments: createCollectionReducer('comment'),
    people: createCollectionReducer('person'),
    ...asyncReducers
  })
}

// ...

Customizing the default reducer

The reducer returned from redux-data-collections is designed to handle specific actions. For most usecases these default actions will allow you to work smoothly with the collections. However, if we need custom actions we can join the collectionReducer from redux-data-collections with our custom reducer using reduceReducers.

import createCollectionReducer, { reduceReducers } from 'redux-data-collections'
import { handleActions } from 'redux-actions'
import { CUSTOM_ACTION_TYPE } from './constants'

// create a redux-data-collections reducer
const collectionReducer = createCollectionReducer('media')

// create your own reducer
const mediaReducer = handleActions({
  [CUSTOM_ACTION_TYPE]: state => state
}, {})

// join the two reducers together
// the collection reducer will run first, it will capture only redux-data-collections actions
// the media reducer runs second and will get the state after redux-data-collections has altered it
const reducer = reduceReducers(collectionReducer, mediaReducer)

export default reducer

Send actions to your reducers

import { connect } from 'react-redux'
import EditEntity from '../components/EditEntity'
import { setAttribute } from 'redux-data-collections/lib/actions/item'

const mapStateToProps = (state, ownProps) => {
  return {
  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    setAttr: (attribute, value) => dispatch(
      setAttribute({
        type: 'post',
        id: 'post-id-1',
        attribute,
        value
      })
    )
  }
}

const EditEntityContainer = connect(mapStateToProps, mapDispatchToProps)(EditEntity)

export default EditEntityContainer

Select value from the state

import { connect } from 'react-redux'
import EditEntity from '../components/EditEntity'
import { selectItemAttributes, selectItemAttributeByName } from 'redux-data-collections/lib/selectors/item'
import { setAttribute } from 'redux-data-collections/lib/actions/item'

const mapStateToProps = (state, ownProps) => {
  return {
    attributes: selectItemAttributes(state)('post', 'post-id-1'), // <-- all attributes
    name: selectItemAttributeByName(state)('post', 'post-id-1', 'name'), // <-- just the name
  }
}

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    setName: (value) => dispatch(
      setAttribute({
        type: 'post',
        id: 'post-id-1',
        attribute: 'name',
        value
      })
    )
  }
}

const EditEntityContainer = connect(mapStateToProps, mapDispatchToProps)(EditEntity)
export default EditEntityContainer