Modularize Redux by dynamically loading reducers and middlewares.
Перейти к файлу
Alex Bettadapur 1ac13cc1fd Add docs on ref counting 2018-10-08 10:55:27 -07:00
.vscode Allow adding middlewares via a module (#8) 2018-10-04 21:12:53 -07:00
docs Add docs on ref counting 2018-10-08 10:55:27 -07:00
packages Fix observable externals 2018-10-08 10:17:44 -07:00
.gitignore Initial checking 2018-08-31 20:22:27 -07:00
CNAME Add CNAME 2018-10-05 19:58:03 -07:00
LICENSE Initial checking 2018-08-31 20:22:27 -07:00
README.md Fix typo (#10) 2018-10-07 07:57:15 -07:00
lerna.json Refactor some code 2018-10-04 14:41:04 -07:00
package-lock.json save 2018-10-05 18:50:59 -07:00
package.json remove docsify from deps 2018-10-05 19:45:47 -07:00

README.md

logo

Redux Dynamic Modules

What is it?

redux-dynamic-modules is a library that aims to make Redux Reducers easy to modularize and add/remove dynamically.

Motivation

In large React/Redux applications, oftentimes you will have portions of your state that serve distinct purposes. For example, you might have a reducer and saga that manages LoginState in your application, or another set that manages Todos. These can be split up into a LoginModule and a TodoModule.

Modules provide the following benefits:

  • They can be easily re-used across the application, or between multiple similar applications.
  • They can be added/removed from the store dynamically, ex. when a component mounts or when a user performs an action

Features

  • Group together reducers, middleware, and state into a single, re-usable module.
  • Add and remove modules from a Redux store at any time.
  • Use the included <DynamicModuleLoader /> component to automatically add a module when a component is rendered
  • Extensions provide integration with popular libraries, including redux-saga and redux-observable

Install

Run

npm install redux-dynamic-modules

or

yarn add redux-dynamic-modules

Usage

  • Create a module with the following format
export function getUsersModule(): IModule<IUserState> {
  return {
    id: "users",
    reducerMap: {
      users: usersReducer
    },
    sagas: [usersSaga],

    // Actions to fire when this module is added/removed
    // initialActions: [],
    // finalActions: []
  }
}

  • Create a ModuleStore
import {configureStore, IModuleStore} from "redux-dynamic-modules";
import {getUsersModule} from "./usersModule";

const store: IModuleStore<IState> = configureStore(
  /* initial state */
  {},

  /* redux-saga context */ 
  {},

  getUsersModule(), 
  /* ...any additional modules */
);
  • Use like a standard Redux store
  • Use the DynamicModuleLoader React component to add/remove modules when components mount/unmount
<DynamicModuleLoader reduxModules={modules}>
   <div>Hello World!!</div>
</DynamicModuleLoader>

Examples

See the Todo App for a quick of example of the library in practice.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.