Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redux-dynamic-reducer

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-dynamic-reducer

Create Redux stores that can have additional reducers dynamically attached at runtime

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
36
decreased by-47.83%
Maintainers
1
Weekly downloads
 
Created
Source

redux-dynamic-reducer

npm version npm downloads License: MIT

This is a library to create Redux stores that can have additional reducers dynamically attached at runtime.

It is based on an example proposed by Dan Abramov in a StackOverflow answer.

What it does

Redux only supports a single root reducer for the store. When designing the store structure, combineReducers can be used to combine multiple reducers into a single reducer for the store. However, you cannot add more reducers to the combination after the store has been created.

That's where this library fits in. It allows you to provide a single root reducer but also provide additional reducers to be merged into the root reducer after the store is created.

It also provides a useful utilities to package a component with a related reducer and attach it when the component is mounted.

When to use it

  • You do not know which reducers are required when creating the store.
  • You want to split your bundle and some reducers will only be available after the import is resolved.

How to use

1. Create the store

The createStore function can be used to create store that can have reducer dynamically attached. It is a drop-in replacement for the built-in Redux version:

import { combineReducers } from 'redux'
import { createStore } from 'redux-dynamic-reducer'

...

const reducer = combineReducers({ staticReducer1, staticReducer2 })
const store = createStore(reducer)
Initial State and Middleware

The createStore function also supports all of the optional parameters that the built-in Redux createStore function does:

const store = createStore(reducer, { initial: 'state' })
const store = createStore(reducer, applyMiddleware(middleware))
const store = createStore(reducer, { initial: 'state' }, applyMiddleware(middleware))

2. Add a dynamic reducer

The store now has a new function on it caller attachReducers:

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:

store.attachReducers({
    some: {
        path: {
            to: {
                dynamicReducer
            }
        }
    }
} )
store.attachReducers({ 'some.path.to': { dynamicReducer } } } })
store.attachReducers({ 'some/path/to': { dynamicReducer } } } })

Examples

Examples can be found here.

Limitations

  • Each dynamic reducer needs a unique key
    • If the same key is used in a subsequent attachment, the original reducer will be replaced
  • Nested reducers cannot be attached to nodes of the state tree owned by a static reducer

FAQs

Package last updated on 04 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc