randallknutson / redux-injector Goto Github PK
View Code? Open in Web Editor NEWAllows dynamically injecting reducers into a redux store at runtime
Allows dynamically injecting reducers into a redux store at runtime
Great job on redux-injector. Super useful. Here's an idea for a utility function to dynamically add reducers on the fly. I was shocked at how simple this was to do.
//used in conjunction with redux-injector
//given a plain object: reducer = {}
//the reducer factory enables adding reducer functions dynamically
export function ReducerFactory(initialStoreState, reducer) {
return function reducerFunction(state = initialStoreState, action) {
if (reducer[action.type]) {
return reducer[action.type](state, action);
} else {
return state
}
}
}
/*EXAMPLE USE
import { ReducerFactory } from './reducerFactory'
import { injectReducer } from './redux-injector';
createReducers(propsID, initialStoreState) {
reducer = {};
injectReducer(propsID, ReducerFactory(initialStoreState, reducer));
this.reducer[`${propsID}_SUCCESS`] = (state, action) => {
return Object.assign({}, state, { error: null, isPending: false })
}
this.reducer[`${propsID}_PENDING`] = (state, action) => {
return Object.assign({}, state, { isPending: true });
}}
this.reducer[`${propsID}_SUCCESS`] = (state, action) => {
return Object.assign({}, state, { error: null, isPending: false, payload: action.payload })
}
}
*/
We use this package in development and production. I wonder if it is possible to use this together with Redux Devtools somehow?
I've tried passing composeEnhancers()
to the createInjectStore()
function, and it seems to work initially, but then Redux Devtools crashes.
Thank you for creating this package, it has been very useful to us.
if i need isomorphic , how i do ? ๐
Hey there,
Thanks for this amazing lib, and especially the recursive reducer injection ๐
I'm using redux-sagas too, and I've created a lib for loading sagas asynchronously, it uses redux-injector as a dependency for creating the store.
It can be found here: https://github.com/GuillaumeCisco/redux-sagas-injector
An exemple is available in the README ๐พ
Hey there,
I see in the source code:
export function injectReducer(key, reducer, force = false) {
// If already set, do nothing.
if (has(store.injectedReducers, key) || force) return;
set(store.injectedReducers, key, reducer);
store.replaceReducer(combineReducersRecurse(store.injectedReducers));
}
What is the purpose of the force
parameter?
If injectedReducers
has the reducer key, it won't replace it
If we force it by settings force
to true
, it won't replace it either.
Should not the force
parameter set to true replace the reducer even if injectedReducers
has it?
If you want to write a Redux store setup library module to be used in multiple applications, then it would make sense to support creating it without initial reducers. Currently you must provide at least on dummy no-op reducer.
Implementation proposal: when initialReducers
is falsy then a no-op reducer is installed.
function combineReducersRecurse(reducers) {
// leaf provides no reducer, use a no-op reducer
if (!reducers) {
return () => ({});
}
...
}
export function createInjectStore(initialReducers, ...args) {
...
store.injectedReducers = initialReducers || {};
return store;
}
How do i call replaceReducer on the store with this library?
Would it not be possible to make the injection work with the store that is on context
? That way, this works with server-side rendering. So there'd be a decorator that you pass the reducer to and it injects it on Reduxโฆ
I am trying to reset my store using the method shown here -> https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.