Skip to content

Latest commit

 

History

History
 
 

redux-dynostore-core

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@redux-dynostore/core

build status npm version npm downloads License: BSD-3-Clause

Core library to add dynamic enhancers to redux stores.

Usage

import dynostore from '@redux-dynostore/core'

const store = createStore(reducer, dynostore(
  dynamicEnhancer(),
  dynamicEnhancer2('with parameters')
))

Enhancers

Dynamic enhancers are used to make dynamic features available to the store. The following dynamic enhancers are provided:

  1. Reducers - dynamically attach reducers
  2. Sagas - dynamically run sagas

dynamicReducers

import dynostore, { dynamicReducers } from '@redux-dynostore/core'

const store = createStore(reducer, dynostore(dynamicReducers()))

Manually attaching reducers

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
      }
    }
  }
})

Detaching reducers

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.

Custom Enhancers

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.

Utilities

attachReducer

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)

dispatchAction

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)