redux-injector

raw JSON →
0.1.0 verified Fri May 01 auth: no javascript maintenance

A library for dynamically injecting reducers into a Redux store at runtime, enabling lazy loading of reducers and plugin architectures. Version 0.1.0 is the only stable release. Unlike static combineReducers, it uses createInjectStore to accept an uncombined reducer tree and injectReducer to add reducers by path (e.g., 'date.form'). Relies on lodash.set. Release cadence is unknown; last update was years ago. Alternative to static reducer composition or other dynamic Redux solutions like Redux Toolkit's reducer injection examples. Minimal documentation and no recent maintenance, so consider compatibility with modern Redux carefully.

error Uncaught Error: Reducer "..." returned undefined during initialization.
cause You passed a combined reducer (result of combineReducers) to createInjectStore instead of a plain object tree.
fix
Pass the uncombined reducer object tree, not the result of combineReducers.
error TypeError: Cannot read property 'set' of undefined
cause Missing lodash dependency (lodash.set not available).
fix
Install lodash: npm install lodash
error TypeError: state is not a function
cause Trying to use injectReducer on a store created with regular createStore, not createInjectStore.
fix
Use createInjectStore from redux-injector instead of createStore.
gotcha injectReducer uses lodash.set so you must ensure path supports dot notation; missing intermediate objects are created automatically.
fix Be careful with path strings; they follow lodash.set conventions.
gotcha Do NOT combine reducers with combineReducers before passing to createInjectStore; pass the plain object tree instead.
fix Pass an object of reducer functions without combineReducers.
deprecated Package has not been updated in years; may be incompatible with latest Redux and React versions.
fix Consider alternatives like Redux Toolkit's dynamic reducer injection or useReducer-based solutions.
gotcha injectReducer mutates the store's reducer map and may cause subtle bugs if injected after reducers have been combined.
fix Inject before any complex state changes or use immutable injection patterns.
npm install redux-injector
yarn add redux-injector
pnpm add redux-injector

Demonstrates creating a store with createInjectStore and dynamically injecting a reducer using injectReducer with a dot path.

import { createStore, combineReducers } from 'redux';
import { createInjectStore, injectReducer } from 'redux-injector';

// Reducer tree (uncombined)
const reducersObject = {
  counter: (state = 0, action) => {
    switch (action.type) {
      case 'INCREMENT': return state + 1;
      default: return state;
    }
  }
};

// Create store with inject capability
const store = createInjectStore(reducersObject, {});

// Later, inject a new reducer
injectReducer('todos', (state = [], action) => {
  switch (action.type) {
    case 'ADD_TODO': return [...state, action.payload];
    default: return state;
  }
});

store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'ADD_TODO', payload: 'Learn redux-injector' });
console.log(store.getState());
// Output: { counter: 1, todos: ['Learn redux-injector'] }