React Redux Module
IMPORTANT NOTICE
Experimental I didn't realise publishing this package to NPM would cause so many downloads. This package is still experimental so I would not currently recommend it for production use yet though I intend for it to production ready in the coming days.
Some middleware may not work with this library as all actions are pushed through each middleware in each module, so some middleware may not handle this scenario gracefully, though thunks and redux-observable have been tested.
Motivation
Having to compile a list of possible reducers, state and/or middleware ahead-of-time is error prone (you forget to add a reducer/middleware) and time consuming (hunting down which reducers and middleware you need).
This has been resolved by using the redux-modules-enhancer that adds support for adding/removing modules after the store has been created.
In order to avoid polluting JSX code with boilerplate code for adding modules such as,
{ superprops context; thiscontextstore; } ... MyClasscontextTypes store: storeShaperequired
this module provides a ReduxModule component that lets you add a module as part of the render step, for example,
;;; { return <div> ... </div> } ... mapStateToProps & mapDispatchToProps ... mapStateToProps mapDispatchToProps ;
How to use it?
The only 2 dependencies required for use are,
- redux-modules-enhancer - use the modulesEnhancer from redux-modules-enhancer when creating your store to add support for adding/removing modules.
- react-redux - use the Provider component to ensure the store is part of the context.
; ; ; ; const store = Redux;
or, if you want to use other enhancers as well,
; const store = Redux;
After that, you just wrap your component using connectModule and a factory method for creating your module,
MyComponent.jsx
;;; { return <div> ... </div> } /* ... mapStateToProps & mapDispatchToProps ... */ mapStateToProps mapDispatchToProps MyComponent;
MyModule.js
const moduleId = moduleId; const initialState = /* ... initial state ... */ const reducer = { // .. reducer logic .. } const middleware = /* ... any middleware required for this module ... */ ; const onload = { /* Any behaviour to take when the module is first loaded. */ } const onunload = { /* Any behaviour to take when the module is removed from the store. */ } return moduleId initialState reducer middleware onload onunload const getModuleDefinition = moduleId: moduleId || `defaultModuleId` options: defaultValue: 1 ...options ; ;
API
connectModules
component;
- moduleDefinitons is an array of objects or an object containing
- moduleId The unique identifier for the module instance.
- options The options (if any) to be passed to the factory.
- factory A factory method for creating a new module instance with the signature (moduleId, options) => ReduxModuleObject;
- component is the component to be wrapped that relies on the given modules being loaded.
The method returns a Component that wraps the given component and will load the given modules when the component is constructed.