redux-named-reducers
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
; //define all the state you will access in initial stateconst initialState = state1: "default1" state2: "default2"; { //reducer logic goes here} const moduleA = ;
Add the enhancer
;;; const store = ;
Access the state from anywhere in your code
;; { const moduleName = moduleAmoduleName; const state1 = ; const state2 = ;}
Use it to directly access state in mapDispatchToProps()
const mapDispatchToProps = { return { ; } ;};
Alternate method of declaring reducers
Create a named reducer passing an initial state option
; const moduleA = ;
Create a reducer for each action or group of actions using 'reduce' function
//notice no need for '...state' just return the states which changedmoduleA //if you are just changing state to a constant you can return an object directlymoduleA //multiple actions supportedmoduleA //...as well as 'redux-actions' like action creators that have a toString() methodmoduleA //'state' is not given because you can access it using the getState() methodmoduleA
Usage with combineReducers
Just pass the root reducer to the enhancer
;;;; const rootReducer = ; const store = ;
Access external state from other reducers
Add an externalState option with a list of external state with default values
const moduleA = ;
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)
; ;;
You can then access the external state from the local module
const extState1 = ;const 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)
;;; const mySelector =
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
; const derivedState = ;
Notes
getState() currently works for first level state properties only. To access nested state you must do:
subState1;