Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine redux reducers without adding nesting

I have a scenario where I have 2 reducers that are the result of a combineReducers. I want to combine them together, but keep their keys at the same level on nesting.

For example, given the following reducers

const reducerA = combineReducers({ reducerA1, reducerA2 }) const reducerB = combineReducers{{ reducerB1, reducerB2 }) 

I want to end up with a structure like:

{     reducerA1: ...,     reducerA2: ...,     reducerB1: ...,     reducerB2: ... } 

If I use combineReducers again on reducerA and reducerB like so:

const reducer = combineReducers({ reducerA, reducersB }) 

I end up with a structure like:

{     reducerA: {         reducerA1: ...,         reducerA2: ...     },     reducerB: {         reducerB1: ...,         reducerB2: ...     } } 

I can't combine reducerA1, reducerA2, reducerB1 and reducerB2 in a single combineReducers call as reducerA and reducerB are being provided to me already combined from different npm packages.

I have tried using the reduce-reducers library to combine them togethers and reduce the state together, an idea I got from looking at the redux docs, like so:

const reducer = reduceReducers(reducerA, reducerB) 

Unfortunately this did not work as the resulting reducer from combineReducers producers a warning if unknown keys are found and ignores them when returning its state, so the resulting structure only contains that of reducerB:

{     reducerB1: ...,     reducerB2: ... } 

I don't really want to implement my own combineReducers that does not enforce the structure so strictly if I don't have to, so I'm hoping someone knows of another way, either built-in to redux or from a library that can help me with this. Any ideas?


Edit:

There was an answer provided (it appears to have been deleted now) that suggested using flat-combine-reducers library:

const reducer = flatCombineReducers(reducerA, reducerB) 

This was one step closer than reduce-reducers in that it managed to keep the keep the state from both reducerA and reducerB, but the warning messages are still being produced, which makes me wonder if the vanishing state I observed before was not combineReducers throwing it away, but rather something else going on with the reduce-reducers implementation.

The warning messages are:

Unexpected keys "reducerB1", "reducerB2" found in previous state received by the reducer. Expected to find one of the known reducer keys instead: "reducerA1", "reducerA2". Unexpected keys will be ignored.

Unexpected keys "reducerA1", "reducerA2" found in previous state received by the reducer. Expected to find one of the known reducer keys instead: "reducerB1", "reducerB2". Unexpected keys will be ignored.

If I do a production build, the warning disappear (such is the way for many react/redux warnings), but I'd rather them not appear at all.

I've also done some more searching for other libraries and found redux-concatenate-reducers:

const reducer = concatenateReducers([reducerA, reducerB]) 

This has the same result as flat-combine-reducers so the search continues.


Edit 2:

A few people have made some suggestions now but none have worked so far, so here is a test to help:

import { combineReducers, createStore } from 'redux'  describe('Sample Tests', () => {      const reducerA1 = (state = 0) => state     const reducerA2 = (state = { test: "value1"}) => state     const reducerB1 = (state = [ "value" ]) => state     const reducerB2 = (state = { test: "value2"}) => state          const reducerA = combineReducers({ reducerA1, reducerA2 })     const reducerB = combineReducers({ reducerB1, reducerB2 })      const mergeReducers = (...reducers) => (state, action) => {         return /* your attempt goes here */     }      it('should merge reducers', () => {         const reducer = mergeReducers(reducerA, reducerB)          const store = createStore(reducer)          const state = store.getState()          const expectedState = {             reducerA1: 0,             reducerA2: {                 test: "value1"             },             reducerB1: [ "value" ],             reducerB2: {                 test: "value2"             }         }          expect(state).to.deep.equal(expectedState)     }) }) 

The goal is to get this test to pass AND not produce any warnings in the console.


Edit 3:

Added more tests to cover more cases, including handling an action after the initial creation and if the store is created with initial state.

import { combineReducers, createStore } from 'redux'  describe('Sample Tests', () => {      const reducerA1 = (state = 0) => state     const reducerA2 = (state = { test: "valueA" }) => state     const reducerB1 = (state = [ "value" ]) => state     const reducerB2 = (state = {}, action) => action.type == 'ADD_STATE' ? { ...state, test: (state.test || "value") + "B" } : state          const reducerA = combineReducers({ reducerA1, reducerA2 })     const reducerB = combineReducers({ reducerB1, reducerB2 })      // from Javaguru's answer     const mergeReducers = (reducer1, reducer2) => (state, action) => ({         ...state,         ...reducer1(state, action),         ...reducer2(state, action)     })      it('should merge combined reducers', () => {         const reducer = mergeReducers(reducerA, reducerB)          const store = createStore(reducer)          const state = store.getState()          const expectedState = {             reducerA1: 0,             reducerA2: {                 test: "valueA"             },             reducerB1: [ "value" ],             reducerB2: {}         }          expect(state).to.deep.equal(expectedState)     })      it('should merge basic reducers', () => {         const reducer = mergeReducers(reducerA2, reducerB2)          const store = createStore(reducer)          const state = store.getState()          const expectedState = {             test: "valueA"         }          expect(state).to.deep.equal(expectedState)     })      it('should merge combined reducers and handle actions', () => {         const reducer = mergeReducers(reducerA, reducerB)          const store = createStore(reducer)          store.dispatch({ type: "ADD_STATE" })          const state = store.getState()          const expectedState = {             reducerA1: 0,             reducerA2: {                 test: "valueA"             },             reducerB1: [ "value" ],             reducerB2: {                 test: "valueB"             }         }          expect(state).to.deep.equal(expectedState)     })      it('should merge basic reducers and handle actions', () => {         const reducer = mergeReducers(reducerA2, reducerB2)          const store = createStore(reducer)          store.dispatch({ type: "ADD_STATE" })          const state = store.getState()          const expectedState = {             test: "valueAB"         }          expect(state).to.deep.equal(expectedState)     })      it('should merge combined reducers with initial state', () => {         const reducer = mergeReducers(reducerA, reducerB)          const store = createStore(reducer, { reducerA1: 1, reducerB1: [ "other" ] })          const state = store.getState()          const expectedState = {             reducerA1: 1,             reducerA2: {                 test: "valueA"             },             reducerB1: [ "other" ],             reducerB2: {}         }          expect(state).to.deep.equal(expectedState)     })      it('should merge basic reducers with initial state', () => {         const reducer = mergeReducers(reducerA2, reducerB2)          const store = createStore(reducer, { test: "valueC" })          const state = store.getState()          const expectedState = {             test: "valueC"         }          expect(state).to.deep.equal(expectedState)     })      it('should merge combined reducers with initial state and handle actions', () => {         const reducer = mergeReducers(reducerA, reducerB)          const store = createStore(reducer, { reducerA1: 1, reducerB1: [ "other" ] })          store.dispatch({ type: "ADD_STATE" })          const state = store.getState()          const expectedState = {             reducerA1: 1,             reducerA2: {                 test: "valueA"             },             reducerB1: [ "other" ],             reducerB2: {                 test: "valueB"             }         }          expect(state).to.deep.equal(expectedState)     })      it('should merge basic reducers with initial state and handle actions', () => {         const reducer = mergeReducers(reducerA2, reducerB2)          const store = createStore(reducer, { test: "valueC" })          store.dispatch({ type: "ADD_STATE" })          const state = store.getState()          const expectedState = {             test: "valueCB"         }          expect(state).to.deep.equal(expectedState)     }) }) 

The above mergeReducers implementation passes all the tests, but still producers warnings to the console.

  Sample Tests     ✓ should merge combined reducers     ✓ should merge basic reducers Unexpected keys "reducerB1", "reducerB2" found in previous state received by the reducer. Expected to find one of the known reducer keys instead: "reducerA1", "reducerA2". Unexpected keys will be ignored. Unexpected keys "reducerA1", "reducerA2" found in previous state received by the reducer. Expected to find one of the known reducer keys instead: "reducerB1", "reducerB2". Unexpected keys will be ignored.     ✓ should merge combined reducers and handle actions     ✓ should merge basic reducers and handle actions     ✓ should merge combined reducers with initial state     ✓ should merge basic reducers with initial state     ✓ should merge combined reducers with initial state and handle actions     ✓ should merge basic reducers with initial state and handle actions 

It is important to note that the warnings being printed are for the test case immediately after and that combineReducers reducers will only print each unique warning once, so because I'm reusing the reducer between tests, the warnings are only shown for the first test case to produce it (I could combine the reducers in each test to prevent this, but as the criteria I'm looking for it to not produce them at all, I'm happy with this for now).

If you are attempting this, I don't mind if mergeReducers accepts 2 reducers (like above), an array of reducers or an object of reducers (like combineReducers). Actually, I don't mind how it is achieved as long as it doesn't require any changes to the creation of reducerA, reducerB, reducerA1, reducerA1, reducerB1 or reducerB2.


Edit 4:

My current solution is modified from Jason Geomaat's answer.

The idea is to filter the state being provided to the reducer using the keys of previous calls by using the following wrapper:

export const filteredReducer = (reducer) => {     let knownKeys = Object.keys(reducer(undefined, { type: '@@FILTER/INIT' }))      return (state, action) => {         let filteredState = state          if (knownKeys.length && state !== undefined) {             filteredState = knownKeys.reduce((current, key) => {                 current[key] = state[key];                 return current             }, {})         }          let newState = reducer(filteredState, action)          let nextState = state          if (newState !== filteredState) {             knownKeys = Object.keys(newState)             nextState = {                 ...state,                 ...newState             }         }          return nextState;     }; } 

I merge the result of the filtered reducers using the redux-concatenate-reducers library (could have used flat-combine-reducers but the merge implementation of the former seems a bit more robust). The mergeReducers function looks like:

const mergeReducers = (...reducers) => concatenateReducers(reducers.map((reducer) => filterReducer(reducer)) 

This is called like so:

const store = createStore(mergeReducers(reducerA, reducerB) 

This passes all of the tests and doesn't produce any warnings from reducers created with combineReducers.

The only bit I'm not sure about is where the knownKeys array is being seeded by calling the reducer with an INIT action. It works, but it feels a little dirty. If I don't do this, the only warning that is produced is if the store is created with an initial state (the extra keys are not filtered out when resolving the initial state of the reducer.

like image 641
Michael Peyper Avatar asked Apr 08 '17 04:04

Michael Peyper


People also ask

Why do we combine reducers in Redux?

With Redux, there is just one store, but combineReducers helps you keep the same logical division between reducers.

Can we use multiple reducers in Redux?

It turns out that Redux lets us combine multiple reducers into one that can be passed into createStore by using a helper function named combineReducers . The way we combine reducers is simple, we create one file per reducer in the reducers directory.


1 Answers

Ok, decided to do it for fun, not too much code... This will wrap a reducer and only provide it with keys that it has returned itself.

// don't provide keys to reducers that don't supply them const filterReducer = (reducer) => {   let lastState = undefined;   return (state, action) => {     if (lastState === undefined || state == undefined) {       lastState = reducer(state, action);       return lastState;     }     var filteredState = {};     Object.keys(lastState).forEach( (key) => {       filteredState[key] = state[key];     });     var newState = reducer(filteredState, action);     lastState = newState;     return newState;   }; } 

In your tests:

const reducerA = filterReducer(combineReducers({ reducerA1, reducerA2 })) const reducerB = filterReducer(combineReducers({ reducerB1, reducerB2 })) 

NOTE: This does break with the idea that the reducer will always provide the same output given the same inputs. It would probably be better to accept the list of keys when creating the reducer:

const filterReducer2 = (reducer, keys) => {   let lastState = undefined;   return (state, action) => {     if (lastState === undefined || state == undefined) {       lastState = reducer(state, action);       return lastState;     }     var filteredState = {};     keys.forEach( (key) => {       filteredState[key] = state[key];     });     return lastState = reducer(filteredState, action);   }; }  const reducerA = filterReducer2(   combineReducers({ reducerA1, reducerA2 }),   ['reducerA1', 'reducerA2']) const reducerB = filterReducer2(   combineReducers({ reducerB1, reducerB2 }),   ['reducerB1', 'reducerB2']) 
like image 190
Jason Goemaat Avatar answered Oct 06 '22 18:10

Jason Goemaat