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-named-reducers

v1.1.0

Published

Access redux state anywhere in your code through named reducers

Downloads

1,077

Readme

redux-named-reducers

npm

Access redux state anywhere in your code through reducers, and optionally split your reducers into smaller 'reduce' functions that operate per action

Usage

npm install redux-named-reducers --save

Create a named reducer

import { createNamedReducer } from "redux-named-reducers";

//define all the state you will access in initial state
const initialState = {
  state1: "default1",
  state2: "default2"
};

function reducer(state = initialState, action) {
  //reducer logic goes here
}

export const moduleA = createNamedReducer({
  moduleName: "moduleA",
  reducer: reducer
});

Add the enhancer

import { createStore, compose } from "redux";
import { namedReducerEnhancer } from "redux-named-reducers";
import { moduleA } from "./moduleA";

const store = createStore(moduleA, compose(namedReducerEnhancer(moduleA.reducer), ...otherEnhancersOrMiddleware));

Access the state from anywhere in your code

import { getState } from "redux-named-reducers";
import { moduleA } from "./moduleA";

function someFunction() {
  const moduleName = moduleA.moduleName;
  const state1 = getState(moduleA.state1);
  const state2 = getState(moduleA.state2);
}

Use it to directly access state in mapDispatchToProps()

const mapDispatchToProps = dispatch => {
  return {
    onClick: () => {
      dispatch(someAction(getState(moduleA.state1)));
    }
  };
};

Alternate method of declaring reducers

Create a named reducer passing an initial state option

import { createNamedReducer } from "redux-named-reducers";

export const moduleA = createNamedReducer({
  initialState: initialState
});

Create a reducer for each action or group of actions using 'reduce' function

//notice no need for '...state' just return the states which changed
moduleA.reduce(actions.ACTION1, action => {
  state1: action.param1,
  state2: action.param2
})

//if you are just changing state to a constant you can return an object directly
moduleA.reduce(actions.ACTION2, { state1: "nextState"} )

//multiple actions supported
moduleA.reduce([actions.ACTION3, actions.ACTION4], { state2: "nextState"} )

//...as well as 'redux-actions' like action creators that have a toString() method
moduleA.reduce(myActionCreator1, { state1: "nextState"} )

//'state' is not given because you can access it using the getState() method
moduleA.reduce(toggleState1, () => ({
  state1: !getState(moduleA.state1)
})

Usage with combineReducers

Just pass the root reducer to the enhancer

import { combineReducers, createStore, compose } from "redux";
import { namedReducerEnhancer } from "redux-named-reducers";
import { moduleA } from "./moduleA";
import { moduleB } from "./moduleB";

const rootReducer = combineReducers({
  [moduleA.moduleName]: moduleA.reducer,
  [moduleB.moduleName]: moduleB.reducer
});

const store = createStore(rootReducer, compose(namedReducerEnhancer(rootReducer), ...otherEnhancersOrMiddleware));

Access external state from other reducers

Add an externalState option with a list of external state with default values

export const moduleA = createNamedReducer({
  moduleName: "moduleA",
  reducer: reducer,
  externalState: { extState1: null, extState2: "" }
});

In the main app link the external state to other modules (Note: Since version v1.0.7 you must use linkState() to link states, old method is no longer supported)

import { linkState } from "redux-named-reducers";

linkState(moduleA.extState1, moduleB.state1);
linkState(moduleA.extState2, moduleC.state1);

You can then access the external state from the local module

const extState1 = getState(moduleA.extState1);
const extState2 = getState(moduleA.extState2);

Usage with reselect

Each named reducer state is just a selector so you can use it in reselect (since version 1.0.7 only)

import { createSelector } from 'reselect';
import { moduleA } from "./moduleA";
import { moduleB } from "./moduleB";

const mySelector = createSelector(
  [ moduleA.state1, moduleA.extState1, moduleB.state2 ],
  (state1, extState1, state2) => {
    //selector logic goes here  
  }  

You can then use the selector as normal, or if you link it to your state then you can access it anywhere in your code

linkState(moduleA.extState1, mySelector);

const derivedState = getState(moduleA.extState1);

Notes

getState() currently works for first level state properties only. To access nested state you must do:

getState(moduleA.state1).subState1;