|
| 1 | +<div> |
| 2 | +<img src="docs/redux-dynamic-modules.png" alt="logo" width="100"> |
| 3 | +</img> |
| 4 | +<h1>Redux Dynamic Modules</h1< |
| 5 | +</div> |
| 6 | + |
| 7 | +[](https://dev.azure.com/redux-dynamic-modules/redux-dynamic-modules/redux-dynamic-modules%20Team/_build?definitionId=1&_a=summary) [](https://npmjs.org/package/redux-dynamic-modules) [](https://gitter.im/redux-dynamic-modules/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)  |
| 8 | + |
| 9 | +## What is it? |
| 10 | + |
| 11 | +**redux-dynamic-modules** is a library that aims to make Redux Reducers and middleware easy to modular-ize and add/remove dynamically. |
| 12 | + |
| 13 | +## Motivation |
| 14 | + |
| 15 | +In large Javascript applications, it is often desired to perform some kind of code-splitting, so that the initial script size is small. However, in Redux, you are required to define your reducers and middleware up-front; there is no good way to dynamically add/remove these constructs at runtime. |
| 16 | + |
| 17 | +**redux-dynamic-modules** is designed to make dynamically loading these constructs easier. You can define a **module**, which contains reducers and middleware, and add it to the Redux store at runtime. We also provide a React component `DynamicModuleLoader`, which can load/unload modules on mount/unmount. |
| 18 | + |
| 19 | +Modules provide the following benefits: |
| 20 | + |
| 21 | +- Modules can be easily re-used across the application, or between multiple similar applications. |
| 22 | +- Components declare the modules needed by them and redux-dynamic-modules ensures that the module is loaded for the component. |
| 23 | +- Modules can be added/removed from the store dynamically, ex. when a component mounts or when a user performs an action |
| 24 | + |
| 25 | +## Features |
| 26 | + |
| 27 | +- Group together reducers, middleware, and state into a single, re-usable module. |
| 28 | +- Add and remove modules from a Redux store at any time. |
| 29 | +- Use the included `<DynamicModuleLoader />` component to automatically add a module when a component is rendered |
| 30 | +- Extensions provide integration with popular libraries, including `redux-saga` and `redux-thunk` |
| 31 | + |
| 32 | +## Example Scenarios |
| 33 | + |
| 34 | +- You don't want to load the code for all your reducers up front. Define a module for some reducers and use `DynamicModuleLoader` and a library like [react-loadable](https://github.com/jamiebuilds/react-loadable) to download and add your module at runtime. |
| 35 | +- You have some common reducers/middleware that need to be re-used in different areas of your application. Define a module and easily include it in those areas. |
| 36 | +- You have a mono-repo that contains multiple applications which share similar state. Create a package containing some modules and re-use them across your applications |
| 37 | + |
| 38 | +## Install |
| 39 | + |
| 40 | +Run |
| 41 | + |
| 42 | +``` |
| 43 | +npm install redux-dynamic-modules |
| 44 | +``` |
| 45 | + |
| 46 | +or |
| 47 | + |
| 48 | +``` |
| 49 | +yarn add redux-dynamic-modules |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +- Create a module with the following format |
| 55 | + |
| 56 | +```typescript |
| 57 | +export function getUsersModule(): IModule<IUserState> { |
| 58 | + return { |
| 59 | + id: "users", |
| 60 | + reducerMap: { |
| 61 | + users: usersReducer, |
| 62 | + }, |
| 63 | + // Actions to fire when this module is added/removed |
| 64 | + // initialActions: [], |
| 65 | + // finalActions: [] |
| 66 | + }; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +- Create a `ModuleStore` |
| 71 | + |
| 72 | +```typescript |
| 73 | +import { createStore, IModuleStore } from "redux-dynamic-modules"; |
| 74 | +import { getUsersModule } from "./usersModule"; |
| 75 | + |
| 76 | +const store: IModuleStore<IState> = createStore({ |
| 77 | + initialState: { /** initial state */ }, |
| 78 | + enhancers: [ /** enhancers to include */ ], |
| 79 | + extensions: [/** extensions to include i.e. getSagaExtension(), getThunkExtension() */], |
| 80 | +}, |
| 81 | + getUsersModule() |
| 82 | + /* ...any additional modules */ |
| 83 | +); |
| 84 | +``` |
| 85 | + |
| 86 | +- Use like a standard Redux store |
| 87 | +- Use the `DynamicModuleLoader` React component to add/remove modules when components mount/unmount |
| 88 | + |
| 89 | +```jsx |
| 90 | +<DynamicModuleLoader modules={[getHelloWorldModule()]}> |
| 91 | + <div>Hello World!!</div> |
| 92 | +</DynamicModuleLoader> |
| 93 | +``` |
| 94 | + |
| 95 | +## Extensions |
| 96 | + |
| 97 | +- redux-saga - [redux-dynamic-modules-saga](https://www.npmjs.com/package/redux-dynamic-modules-saga) |
| 98 | +- redux-thunk - [redux-dynamic-modules-thunk](https://www.npmjs.com/package/redux-dynamic-modules-thunk) |
| 99 | + |
| 100 | +## Examples |
| 101 | + |
| 102 | +See the [Widgets](https://github.com/Microsoft/redux-dynamic-modules/tree/master/packages/widgets-example) for a quick of example of the library in practice. |
| 103 | + |
| 104 | +Step by step walthrough of start consuming `redux-dynamic-modules` in the widget app. [Youtube](https://www.youtube.com/watch?v=SktRbSZ-4Tk) |
| 105 | + |
| 106 | +## Contributing |
| 107 | + |
| 108 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a |
| 109 | +Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us |
| 110 | +the rights to use your contribution. For details, visit https://cla.microsoft.com. |
| 111 | + |
| 112 | +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide |
| 113 | +a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions |
| 114 | +provided by the bot. You will only need to do this once across all repos using our CLA. |
| 115 | + |
| 116 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 117 | +For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or |
| 118 | +contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
0 commit comments