Delegate one slice of a larger reducer to its own reducer in redux

I have a larger reducer, and I want to add another slice to it that will be fairly complicated unto itself. For example, here’s a large, already complex reducer:

const initialState = {
  bigchunk1: { ...someObject },
  bigchunk2: true,
  bigchunk3: [...somevalues],
  bigchunk4: 'etc'
}

function mainReducer(state = initialState, action){
  switch(action.type){
    case Actions.CASE1:
      return {
        ...state,
        bigchunk1: {
           ...state.bigchunk1;
           somevalue: action.payload.this_is_already_enough_nesting
        }
      }
    case Actions.CASE2:
      return {
        ...state,
        bigchunk4: 'this goes on for awhile'
      }
    // lots more cases
    default:
      return state
  }
}

I want to add a new slice to state, so the whole initialState would end up in the store looking like this:

const initialState = {
  bigchunk1: { ...someObject },
  bigchunk2: true,
  bigchunk3: [...somevalues],
  bigchunk4: 'etc',
  newslice: { ...someComplicatedThing }
}

But rather than have to write all my new cases into my original reducer (along with a very messy level of nesting and spread operators), I want to write a new reducer that handles just the cases pertinent to newslice:

const initialState = { ...someComplicatedThing }

function newSliceReducer(state = initialState, action){
  switch(action.type}{
    // cases here
  }
}

I am aware of combineReducers, but I’m having a hard time thinking of how to apply that in this scenario. combineReducers can take these two reducers and place them as siblings, but how can I combine these so that newSliceReducer and its associated state becomes a child of mainReducer under the name newslice? I feel like this should be simple, but its escaping me right now. I have been reading the redux docs, but I’m not seeing the answer clearly. I do not want to use redux-toolkit or any outside libraries.

127 thoughts on “Delegate one slice of a larger reducer to its own reducer in redux”

  1. Howdy just wanted to give you a quick heads up.
    The text in your post seem to be running off the screen in Internet explorer.
    I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let you know.
    The style and design look great though! Hope you get the issue
    solved soon. Many thanks https://hhydroxychloroquine.com/

    Reply
  2. It’s the best time to make some plans for the longer term and
    it’s time to be happy. I have read this put up and if I may I
    want to counsel you few fascinating issues or tips.

    Maybe you could write next articles referring to this article.
    I want to learn even more things about it!

    Reply
  3. Neat blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your theme. Cheers

    Reply
  4. Pingback: ic stromectol 6 mg
  5. Hi there! This article couldn’t be written any better!
    Reading through this article reminds me of my previous roommate!
    He constantly kept talking about this. I am going to forward
    this post to him. Fairly certain he will have a great read.

    Thank you for sharing!

    Reply

Leave a Comment