Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
anubhavgupta opened this issue Jul 31, 2019 · 2 comments

Comments

@anubhavgupta
Copy link
Contributor

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.

@anubhavgupta
Copy link
Contributor Author

Raised a PR which will fix this issue.

anubhavgupta pushed a commit to anubhavgupta/redux that referenced this issue Jul 31, 2019
timdorr added a commit that referenced this issue Aug 12, 2019
* Fixed combineReducers changeDetection logic(#3488)

* Cleaning up these and some other tests while I'm here


Co-authored-by: gupta <guptanub@amazon.com>
Co-authored-by: Tim Dorr <timdorr@users.noreply.github.com>
@reduxjs reduxjs deleted a comment from lileilei Aug 21, 2019
@markerikson
Copy link
Contributor

markerikson commented Oct 29, 2019

Given that #3490 has been merged, this can be closed.

webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
* Fixed combineReducers changeDetection logic(reduxjs#3488)

* Cleaning up these and some other tests while I'm here


Co-authored-by: gupta <guptanub@amazon.com>
Co-authored-by: Tim Dorr <timdorr@users.noreply.github.com>
Former-commit-id: 0435ce1
Former-commit-id: 4d8662c
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
* Fixed combineReducers changeDetection logic(reduxjs#3488)

* Cleaning up these and some other tests while I'm here


Co-authored-by: gupta <guptanub@amazon.com>
Co-authored-by: Tim Dorr <timdorr@users.noreply.github.com>
Former-commit-id: 0435ce1
Former-commit-id: 4d8662c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants