reedux
A really simple approach to managing Redux store paths and reducers
Last updated 3 years ago by silviu.marian .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install reedux 
SYNC missed versions from official npm registry.

Reedux

As applications grow, there's the idea to move away from one large combined reducer, and break down code into modules that depend on a store export

npm downloads Build Status License: MIT dependencies Status devDependencies Status peerDependencies Status

This is another implementation of Dynamic Reducers Loading. Chances are you already built one yourself. Other people did. If you did too, you are not alone

This library is:

  • arguably the simplest solution
  • can be added late into a project at zero cost
  • requires no modifications to the existing project
  • can cope with heavy tooling (think async middlewares)
  • compatible with all of the official dev tools
  • will not change the behavior of Redux in any way
  • declarative, easy to use - the same syntax that you're familiar with
  • supports and promotes ducks-modular-redux / re-ducks
  • supports adding reducers by type - you can specify the action type as the first parameter when registering new reducers
  • supports HMR without storing extra information on the store
  • supports server-side rendering with preloaded state
  • does one thing, and one thing only
  • no runtime dependencies
  • small footprint, under 100 lines of code
  • fully tested
  • licensed under MIT, completely free to use

There is a peer dependency of Redux 3 and above, and a soft dependency on WeakMaps. Most projects already have support for it via babel-polyfill but if yours doesn't, here is an excellent polyfill

API

reedux(object store, [function existingReducer])
  • the default export in the lib
  • store is the store to be managed
  • existingReducer should be supplied if the given store already has a reducer
  • returns a storePath() function for the given store

storePath(string name, any initialState)
  • registers a new store path in your current store
  • the store path will be populated with initialState at the end of the call
  • returns reducer() function for the given store path

reducer([string type], function reducer)
  • registers a reducer

Example

Suppose you already have store.js exporting a Redux store:

// import it
import { createStore } from 'redux'; 

// get your store
const store = createStore(s => s);
export default store;

All you have to do now is write someModule.js

// use it on top of your store
import reedux from 'reedux';
import store from './store.js';

const storePath = reedux(store);
const reducer = storePath('customers', []);

// now you can add a reducer
reducer((customers, action) => {
  switch(action.type) {
    case 'addCustomer':
      return [...customers, action.customerData];
    case 'deleteCustomer': 
      return [...customers.filter(customer => customer.id !== action.customerId)];
    default:
      return customers;
  }
});

Alternative syntax

reducer('addCustomer', (customers, action) => 
  ([...customers, action.customerData]));

reducer('deleteCustomer', (customers, action) => 
  ([...customers.filter(customer => customer !== action.customerId)]));

Limitations

  • Deleting reducers or store paths is not supported

Similar efforts

Current Tags

  • 0.1.3                                ...           latest (3 years ago)

14 Versions

  • 0.1.3                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.0.13                                ...           3 years ago
  • 0.0.12                                ...           3 years ago
  • 0.0.11                                ...           3 years ago
  • 0.0.10                                ...           3 years ago
  • 0.0.9                                ...           3 years ago
  • 0.0.8                                ...           3 years ago
  • 0.0.7                                ...           3 years ago
  • 0.0.6                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (5)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |