Socket
Socket
Sign inDemoInstall

redux-msr

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redux-msr

Merge sub reducers wihtout splitting the Redux store


Version published
Maintainers
1
Install size
11.0 kB
Created

Readme

Source

Redux MSR (Merge Sub Reducers)

js-semistandard-style

Merge sub-reducers while sharing the same Redux store (or slice), enabling scalable reducer code management and reuse.

The Problem

How best to manage file sizes, reducer layout, code reuse, etc. as reducer complexity and/or size increases. Even when using Redux's combineReducers() API method to split and organise your (global) store, resource reducers can still grow and become difficult to maintain. This problem can also be further amplified when adopting the ducks practice.

The Solution

Assume an app which at some point includes counter-based functionality (detail omitted for brevity)

Note: The below example is also using the ducks practice for organising action creators, constants and the root resource-reducer.

We can take the following extremely simplified example:

const INCREMENT = 'COUNTER.INCREMENT.VALUE';
const DECREMENT = 'COUNTER.DECREMENT.VALUE';

const defaultState = 0;

export const incrementValue = () => ({
  type: INCREMENT
});

export const decrementValue = () => ({
  type: DECREMENT
});

export default (prevCounterState = defaultState, action) => {
  switch (action.type) {
    case INCREMENT:
      // ...
      // ... other complicated or lengthy code
      // ...
      return prevCounterState + 1;
      case DECREMENT:
      // ...
      // ... other complicated or lengthy code
      // ...
      return prevCounterState - 1;
    // other case(s)
    // .
    // .
    // .
    default:
      return prevCounterState;
  }
};

And extract/refactor into something like:

const defaultState = 0;

import combineSubReducers from 'redux-msr';
import incrementReducer from './reducers/incrementReducers';
import decrementReducer from './reducers/decrementReducers';

const INCREMENT = 'COUNTER.INCREMENT.VALUE';
const DECREMENT = 'COUNTER.DECREMENT.VALUE';

export const incrementValue = () => ({
  type: INCREMENT
});

export const decrementValue = () => ({
  type: DECREMENT
});

export default combineSubReducers(defaultState, {
  [INCREMENT]: incrementReducer,
  [DECREMENT]: decrementReducer
});

Table of Contents

Installation

This module is distributed via npm and should be included as part of your package's dependencies:

npm install --save redux-msr

Usage

The main method exported by this module has the following type signature:

combineSubReducers(defaultState: any, reducerConfig: object) => ReducerFn

where,

  • defaultState - The default state to use when the Redux store is initialised
  • reducerConfig - An object whose key-value pairs correspond to the action type and corresponding value to return for that action type, respectively. If the return value is a function, then this will be called with the standard Redux reducer parameters, namely <T>(previousState: T, action: object) => T.

The reducer returned by this module will return the previous state unless a specific action type is noted, and a subsequent value or function is supplied in order to change what the new (immutable) state should be. If you need to overwrite the default response of the resulting reducer, i.e. if no action-types match the currently dispatched action, then simply add the required value/function to the default key within the reducerConfig object. For example,

export default combineSubReducers(defaultState, {
  default: myDefaultReducer
});

Examples

Return basic primitive types for a given action type:

// reducer.js
// State is a Number
import combineSubReducers from 'redux-msr';

const defaultState = 0;

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': 4,
  'EXAMPLE.ACTION.TYPE.2': 20,
});
// reducer.js
// State is a String
import combineSubReducers from 'redux-msr';

const defaultState = '';

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': 'monday',
  'EXAMPLE.ACTION.TYPE.2': 'tuesday',
});

Use a refactored sub-reducer:

// reducer.js
// State is a Number
import combineSubReducers from 'redux-msr';

const defaultState = 0;

export default combinSubReducers(defaultState, {
  'EXAMPLE.ACTION.TYPE.1': handleType1Reducer,
  'EXAMPLE.ACTION.TYPE.2': handleType2Reducer,
});

Where handleType1Reducer and handleType2Reducer have the form:

// reducers/handleType1Reducer.js, or
// reducers/handleType2Reducer.js
export default (prevState, action) => {
  // single action type reducer code
};

Keywords

FAQs

Last updated on 12 Jul 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc