Core library to add dynamic enhancers to redux stores.
import dynostore from '@redux-dynostore/core'
const store = createStore(reducer, dynostore(
dynamicEnhancer(),
dynamicEnhancer2('with parameters')
))
Dynamic enhancers are used to make dynamic features available to the store. The following dynamic enhancers are provided:
- Reducers - dynamically attach reducers
- Sagas - dynamically run sagas
import dynostore, { dynamicReducers } from '@redux-dynostore/core'
const store = createStore(reducer, dynostore(dynamicReducers()))
If you aren't using react, or want to attach a reducer outside of the component lifecycle, the store now has an attachReducers
function that can be used to add additional reducers after the store has been created:
store.attachReducers({ dynamicReducer })
Multiple reducers can be attached as well:
store.attachReducers({ dynamicReducer1, dynamicReducer2 })
Reducers can also be added to nested locations in the store. The following formats are supported:
store.attachReducers({ 'some.path.to': dynamicReducer })
store.attachReducers({ 'some/path/to': dynamicReducer })
store.attachReducers({
some: {
path: {
to: {
dynamicReducer
}
}
}
})
If you need to remove a reducer from the store, the detachReducers
function that can be used:
store.detachReducers(['dynamicReducer'])
Multiple reducers can be detached at the same time as well:
store.detachReducers(['dynamicReducer1', 'dynamicReducer2'])
Nested reducers can also be removed by using the full path to the reducer. The following formats are supported:
store.detachReducers(['some.path.to.dynamicReducer'])
store.detachReducers(['some/path/to/dynamicReducer'])
Note: only reducers that were added using an the attachReducer
function can be detached. Static reducers cannot be detached from the store.
Dynamic enhancers can be created for many use cases by implementing the following interface:
const enhancer = createHandlers => (store, reducer, preloadedState) => ({ ...handlers })
handlers
is an object with all the functions you want your enhancer to add to the store. You should only ever append your handlers to the object and not remove any added by other dynamic handlers.
An enhancer compatible with react-redux-dynostore
to attach the reducer when activated:
import dynamic from '@redux-dynostore/react-redux'
import { attachReducer } from '@redux-dynostore/core'
export default dynamic('identifier', attachReducer(myReducer))(MyComponent)
An enhancer compatible with react-redux-dynostore
to dispatch an action when activated:
import dynamic from '@redux-dynostore/react-redux'
import { dispatchAction } from '@redux-dynostore/core'
export default dynamic('identifier', dispatchAction({ type: 'MY_ACTION' }))(MyComponent)