• Stars
    star
    230
  • Rank 170,748 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Redux store with injectable reducers for use with bundle splitting, large apps, and SPAs.

redux-injectable-store

Redux store with injectable reducers for use with bundle splitting, large apps, and SPAs.

Motivation

It is often desirable to have a single "global" redux store in your application so it is easy for reducers to listen and react to actions issued by disparate parts of the application. For sufficiently large applications though, this becomes problematic because you then need to require all of the reducers in your entire app at the time of creating your store. This gets more complicated once you start bundle splitting, or lazily loading more JavaScript in the context of a SPA.

This library allows reducers to "inject" themselves into the store at any time, solving the above problems.

Installation

npm i --save redux-injectable-store

API

Right now, this module exports a single method: createInjectableStore. You can use in a CommonJS environment:

ES6:

import { createInjectableStore } from 'redux-injectable-store';

CommonJS:

const createInjectableStore = require('redux-injectable-store').createInjectableStore;

createInjectableStore has the following API (extending Redux's flow types)

// This is your typical redux store (returned by `createStore(...)`
type ReduxStore<S, A> = {
  dispatch: Dispatch<A>;
  getState(): S;
  subscribe(listener: () => void): () => void;
  replaceReducer(nextReducer: Reducer<S, A>): void
};

// This is a new "injectable" store with some extra methods
type InjectableStore<S, A> = ReduxStore<S, A> & {
  inject(namespace: string, reducer: Reducer<S, A>, force: boolean = false),
  injectAll({ [key: string]: Reducer<S, A> }, force: boolean = false),
  clearReducers(),
};

function createInjectableStore<S, A>(
  preloadedState: S, 
  enhancer: StoreEnhancer<S, A>,
  wrapReducer: (Reducer<S, A>): Reducer<S, A> = Identity
): InjectableStore;

The createInjectableStore API follows the same API as Redux's createStore, but with the first argument (reducer) missing, and an optional additional last argument, wrapReducer. The wrapReducer argument is a function that takes a reducer and returns a reducer. It is meant to allow you to provide some global action handling if you need to.

Usage

// store.js

import { createInjectableStore } from 'redux-injectable-store';
import { applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import pack from 'redux-pack';

const initialState = getInitialStoreState(); // can also just be `{}`

// add whatever middleware you normally would (this is an example)
const enhancer = applyMiddleware(
  thunk,
  pack.middleware
);

const store = createInjectableStore({
  initialState,
  enhancer,
});

export default store;
// SomeOtherFileA.js

import store from '../path/to/store';
import FooReducer from '../path/to/FooReducer';

store.inject('foo', FooReducer);
// SomeOtherFileB.js

import store from '../path/to/store';
import BarReducer from '../path/to/BarReducer';
import QooReducer from '../path/to/QooReducer';

store.injectAll({
  bar: BarReducer,
  qoo: QooReducer,
});

Additional Notes

  1. If you try to inject a reducer into a namespace that already has a reducer without passing the 'force' argument, it will console.warn and be a "no op".

License

MIT

More Repositories

1

react-primitives

Primitive React Interfaces Across Targets
JavaScript
3,085
star
2

redux-pack

Sensible promise handling and middleware for redux
JavaScript
1,333
star
3

react-native-parallax-view

Parallax view for vertical scrollview/listviews with a header image and header content
JavaScript
1,284
star
4

react-native-segmented-view

Segmented View for React Native (with animation)
JavaScript
187
star
5

redux-entity

WIP. An abstraction layer around handling normalized entity storage and data fetching with redux
JavaScript
187
star
6

compose-dogfooding

Code written during the Compose Dogfooding streams
Kotlin
183
star
7

react-native-pan-controller

A react native component to help with common use cases for scrolling/panning/etc
JavaScript
181
star
8

recoil

Swift and Kotlin ports of React (Prototype)
Swift
180
star
9

dolla

A light-weight jQuery clone just cuz
JavaScript
139
star
10

enzyme-example-mocha

Example project with React + Enzyme + Mocha
JavaScript
132
star
11

react-native-safe-module

A safe way to consume React Native NativeModules
JavaScript
118
star
12

enzyme-example-react-native

Example project with React Native + Enzyme
Objective-C
87
star
13

native-navigation-boilerplate

Objective-C
82
star
14

enzyme-example-karma-webpack

Example project with React + Enzyme + Karma + Webpack
JavaScript
82
star
15

react_native_animation_examples

JavaScript
80
star
16

react-native-in-depth

Courseware and material for a class on React Native
Objective-C
74
star
17

react-native-future

Some thoughts on future directions and APIs for React Native
67
star
18

enzyme-example-jest

Example project with React + Enzyme + Jest
JavaScript
45
star
19

BQL

BQL: A Better Query Language, SQL Superset
C#
43
star
20

react-image-magnifier

A react component that accepts a high-res source image and produces a magnifier window on mouse hover over the part of the image the cursor is over
JavaScript
39
star
21

LukeMapper

Lucene.Net Document to Object ORM Mapper inspired by Dapper
C#
36
star
22

react-validators

Enhanced React Shape PropType Validators
JavaScript
36
star
23

knockout-react

A wrapper / bridge for using React.js with Knockout and Knockout with React.js
JavaScript
25
star
24

gestio

Declarative DOM-Based Gesture Responder System
JavaScript
21
star
25

style-equal

An efficient equality algorithm for React Native inline styles
JavaScript
20
star
26

astrobin-compose

Kotlin
20
star
27

knockout-components

A components library for Knockout.js (3.0 and above)
JavaScript
18
star
28

knockout-paged

Knockout Extension/Plugin for common paged-data scenarios
CSS
16
star
29

knuckles.js

A web application framework built on top of Knockout.js
JavaScript
13
star
30

react-native-animated-navigator

React Native's Navigator implemented with the Animated API
JavaScript
9
star
31

thinking-in-react

Courseware and material for a class on React
JavaScript
9
star
32

understanding-javascript

Courseware and material for a class on JavaScript
JavaScript
9
star
33

knockout-oftype

Some Extensions and Helpers for handling the Constructor pattern in Knockout
JavaScript
8
star
34

react-primitives-art

Cross-platform interface for react ART library
JavaScript
8
star
35

lucene-fluent-query-builder

A convenient Fluent-API around building Lucene.Net queries
C#
7
star
36

shallow-element-equals

Efficient shallow equality algorithm that also allows checks for react element equality of children props
JavaScript
7
star
37

babel-preset-react-native

Babel preset for react native... with a few tweaks
JavaScript
6
star
38

enzyme-example-karma

Example project with React + Enzyme + Karma
JavaScript
5
star
39

react-blogger

minimal isomorphic blog engine built on react
JavaScript
3
star
40

mixinjs

A small library to create factories with useful mixins-based inheritence. Inspired by React's createClass method.
JavaScript
3
star
41

murmur2js

An optimized JavaScript implementation of the MurmurHash algorithm.
JavaScript
3
star
42

MiniBlog

Low-Profile blogging framework built in ASP.Net WebPages Framework
JavaScript
3
star
43

keynote-highlight

Tiny web app to help with syntax highlighting code in keynote
HTML
2
star
44

sivi

Automation Software for Siempre Viva Remote Observatory
C++
2
star
45

lelandrichardson.github.io

Source code for intelligiblebabble.com
JavaScript
2
star
46

viki

Viki - The Visual Wiki Platform
JavaScript
2
star
47

funky.js

A functional programming library for JavaScript
JavaScript
1
star
48

gift-wrap

An asset bundler and minifier (CSS, JS, LESS) for ASP.Net MVC projects
C#
1
star
49

native-navigator

A fully "native" and cross platform Navigator for React Native
Objective-C
1
star
50

leap-gesto

JavaScript Gesture API for Leap Motion
JavaScript
1
star
51

Talks

JavaScript
1
star
52

react-native-windowed-listview

Windowed ListView for React Native (experimental)
1
star
53

module-dep-graph

Automatically track a dependency graph of all commonjs modules in an application
JavaScript
1
star
54

relay-global-immutable-store

playing around with a concept of a global store, similar to Relay, with some offline & eventually-consistent features
JavaScript
1
star