Skip to content

State doesn't update when replaceReducers is called with combineReducers and one of the reducers passed to it is removed.  #3488

Closed
@anubhavgupta

Description

@anubhavgupta

Do you want to request a feature or report a bug?
bug

What is the current behavior?
The combineReducers function does't detect if one of the reducers passed to it is removed(via replaceReducers) and thus returns an old state.

// Reducers
const foo = (state = { value: 'foo' } ) => state
const bar = (state = { value: 'bar' }) => state

const ACTION = { type: 'ACTION' };

const originalCompositeReducer = combineReducers({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)


const initialState = store.getState() // returns -> { 
 //                                                   foo: { value: 'foo' }, 
 //                                                   bar: { value: 'bar' } 
 //                                                }

// now remove `foo` reducer
store.replaceReducer(combineReducers({ bar }))
const nextState = store.getState() // returns -> { 
//                                                 foo: { value: 'foo' }, 
//                                                 bar: { value: 'bar' } 
//                                                }
//                      should have returned -> { bar: { value: 'bar' } }
expect(nextState).not.toBe(initialState) // --> this fails

Steps to reproduce:
Open JSFiddle Link:

  1. Click "Dispatch action" button
    -> 2 boxes gets rendered, one of foo and other of bar.
  2. Click "Replace Reducer: remove bar reducer" button
    -> Again, 2 boxes gets rendered, one of foo and other of bar.
    Expected: Only one box should render, i.e. box of bar only.

What is the expected behavior?
The combineReducers function should detect if one or more reducers passed to it is removed(via replaceReducers) and thus should update the store with new state.

Which versions of Redux, and which browser and OS are affected by this issue? Did this work in previous versions of Redux?
Redux Version: V4.0.4
OS: MAC
Worked on previous versions: no.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions