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

v0.0.0

Published

Namespaces for Redux actions, reducers and state.

Downloads

104

Readme

Redux Namespaces

Build Status Coverage Status

Namespace Redux actions, reducers and state.

Usage


Build re-usable components in React, or any other UI library, using Redux adds complexity. Namespacing actions, reducers and state allows multiple instances of components to co-exist with independent state.

Read more: insert article url

Reducers

When handling multiple components and computing derived data an application's reducer must be aware of this nature. Usually when reducing the action it passes some meta data. This meta data sometimes changes the returning state structure.

Using createReducer(), reducers can be written without the need of this meta data. For example, our state has two superhereos and the application sets their side kick. The reducer is written as if it only had a single superhero. So no longer do reducers have to be instance aware. Regardless if it's batman or superman the reducer will set the side kick.

import { createStore } from 'redux';
import { createReducer } from 'redux-namespaces';

// Always set the initial state, see Caveats below
const initial = {
  batman: {},
  superman: {}
};

// Generic superhero reducer
const superheroReducer = (state, action) => {
  switch (action.type) {
    case 'SET_SIDE_KICK':
      return {
        ...state,
        sideKick: action.sideKick,
      };
    default:
      return state;
  }
};

// Make the generic reducer namespace aware
const reducer = createReducer(superheroReducer);
const store = createStore(reducer, initial);

Action Creators

Action creators now don't require the meta data for the reducer. Since the reducer is built for a single superhero actions can now follow the same pattern.

const actions = {
  sideKick(sideKick) {
    return {
      type: 'SET_SIDE_KICK',
      sideKick,
    };
  }
};

Use createActions() with a given state namespace and the actions list to generate action creators.

import { createActions } from 'redux-namespaces';

// Namespace actions
const batman = createActions('batman', actions);
const superman = createActions('superman', actions);

batman and superman can now disptach the same actions to the reducer but state independent.

// Dispatch
store.dispatch(batman.sideKick('Robin'));
store.dispatch(superman.sideKick('Jimmy Olsen'));

store.getState(); // { batman: { sideKick: 'Robin' }, superman: { sideKick: 'Jimmy Olsen'} }

React

Combine Redux Namespaces with React following the usual react-redux setup. However use createActions for mapDispatchToProps().

const mapDispatchToProps = dispatch =>
	bindActionCreators(
		createActions('namespace', {
	      propAction() {
	        return { type: 'action' };
	      }
		})
	);

Caveats


Initial namespaces must be set within the initial state otherwise action creators will dispatch to an undefined property within the state.

API


createActions(namespace, actions)

  • namespace String The namespace for the action creators.
  • actions Object Action creators to namespace.

Returns namespaced action creators.

createReducers(...reducers)

  • reducers Function|[]Function Redux reducer.

Returns wrapped reducer that is namespace aware.

License


BSD-3-Clause

Copyright (c) 2017 9Technology