- Notifications
You must be signed in to change notification settings - Fork111
Modularize Redux by dynamically loading reducers and middlewares.
License
microsoft/redux-dynamic-modules
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation

redux-dynamic-modules is a library that aims to make Redux Reducers and middleware easy to modular-ize and add/remove dynamically.
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.
redux-dynamic-modules is designed to make dynamically loading these constructs easier. You can define amodule, which contains reducers and middleware, and add it to the Redux store at runtime. We also provide a React componentDynamicModuleLoader, which can load/unload modules on mount/unmount.
Modules provide the following benefits:
- Modules can be easily re-used across the application, or between multiple similar applications.
- Components declare the modules needed by them and redux-dynamic-modules ensures that the module is loaded for the component.
- Modules can be added/removed from the store dynamically, ex. when a component mounts or when a user performs an action
- 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-sagaandredux-thunk
- You don't want to load the code for all your reducers up front. Define a module for some reducers and use
DynamicModuleLoaderand a library likereact-loadable to download and add your module at runtime. - 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.
- 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
Run
npm install redux-dynamic-modulesor
yarn add redux-dynamic-modules- Create a module with the following format
exportfunctiongetUsersModule():IModule<IUserState>{return{id:"users",reducerMap:{users:usersReducer,},// Actions to fire when this module is added/removed// initialActions: [],// finalActions: []};}
- Create a
ModuleStore
import{createStore,IModuleStore}from"redux-dynamic-modules";import{getUsersModule}from"./usersModule";conststore:IModuleStore<IState>=createStore({initialState:{/** initial state */},enhancers:[/** enhancers to include */],extensions:[/** extensions to include i.e. getSagaExtension(), getThunkExtension() */],},getUsersModule()/* ...any additional modules */);
- Use like a standard Redux store
- Use the
DynamicModuleLoaderReact component to add/remove modules when components mount/unmount
<DynamicModuleLoadermodules={[getHelloWorldModule()]}><div>Hello World!!</div></DynamicModuleLoader>
- redux-saga -redux-dynamic-modules-saga
- redux-thunk -redux-dynamic-modules-thunk
See theWidgets for a quick of example of the library in practice.
Step by step walthrough of start consumingredux-dynamic-modules in the widget app.Youtube
This project welcomes contributions and suggestions. Most contributions require you to agree to aContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant usthe rights to use your contribution. For details, visithttps://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to providea CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructionsprovided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted theMicrosoft Open Source Code of Conduct.For more information see theCode of Conduct FAQ orcontactopencode@microsoft.com with any additional questions or comments.
About
Modularize Redux by dynamically loading reducers and middlewares.
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.