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

replaceReducer not working when upgrading to version 4 of redux #2943

Closed
sabbiu opened this issue Apr 18, 2018 · 12 comments
Closed

replaceReducer not working when upgrading to version 4 of redux #2943

sabbiu opened this issue Apr 18, 2018 · 12 comments

Comments

@sabbiu
Copy link

sabbiu commented Apr 18, 2018

What is new in redux v4?

My code was working with redux v3.x.
But, when upgrading with redux v4, replaceReducer didn't update the reducers.

Where can I know about how to use replaceReducer with redux v4?

Meanwhile, I will be using redux v3.x

This is the implementation BTW, https://github.com/sabbiu/dynamic-reducers-for-react

@markerikson
Copy link
Contributor

The implementation for replaceReducer is still here at https://github.com/reactjs/redux/blob/4a215fb74167675485df7d6d2fb1197a184cc461/src/createStore.js#L210-L217, and we've got tests that are all presumably passing . What behavior are you seeing? What do you mean by "didn't update the reducers" ?

@sabbiu
Copy link
Author

sabbiu commented Apr 19, 2018

In my store, https://github.com/sabbiu/dynamic-reducers-for-react/blob/master/src/store.js, I am adding a function to store.

store.asyncReducer = {};
store.injectReducer = (key, reducer) => {
    store.asyncReducers[key] = reducer;
    store.replaceReducer(createReducer(store.asyncReducers));
    // redux v3.x gives me new store
    // redux v4 doesn't.. it gives the same old store
    console.log(store.getState());
};

This is my function for createReducer

import { combineReducers } from "redux";
import { reducer as formReducer } from "redux-form";
import PostsReducer from "./reducer_posts";

const createReducer = asyncReducers =>
  combineReducers({
    posts: PostsReducer,
    form: formReducer,
    ...asyncReducers
  });

export default createReducer;

After dynamic loading of component, I pass new reducers as such,

import searchReducer from "../reducers/reducer_search";
import { withReducer } from "../withReducer";

class Search extends Component {
    ...
}

export default withReducer("search", searchReducer)(Search);

This is withReducer function

import React from "react";
import { object } from "prop-types";

const withReducer = (key, reducer) => WrappedComponent => {
  const Extended = (props, context) => {
    context.store.injectReducer(key, reducer);
    return <WrappedComponent {...props} />;
  };

  Extended.contextTypes = { store: object };

  return Extended;
};

export { withReducer };

@joshjg
Copy link

joshjg commented Apr 19, 2018

This is related to Redux DevTools. Disabling the extension fixes this issue for me. Still investigating.

@markerikson
Copy link
Contributor

markerikson commented Apr 25, 2018

I just got a report of this behavior from someone working through my Educative "Practical Redux" course:

Reducers hmr is not working with redux 4. the tutorial uses an older version, but this may be worth mentioning

@joshjg : what did you see in relation to the Redux DevTools ?

@gaearon
Copy link
Contributor

gaearon commented Apr 25, 2018

My guess is this might be related to the change in the action for replacing. Maybe DevTools handles INIT action specially.

@timdorr
Copy link
Member

timdorr commented Apr 25, 2018

Yup

@markerikson
Copy link
Contributor

That's interesting, because reduxjs/redux-devtools#391 and zalmoxisus/redux-devtools-instrument#17 make it look like there were changes already done to handle this.

@ChristianDavis
Copy link

Dev tools was our issue as well after upgrading to v4, it's really tough to troubleshoot. Just implementing an action logger middleware till a release from either team

export const loggerMiddleware = store => next => action => {
  console.group(action.type);  // eslint-disable-line no-console
  console.info('dispatching', action);  // eslint-disable-line no-console
  const result = next(action);
  // OMIT toJS if you're not using immutable
  console.log('next state', store.getState().toJS()); // eslint-disable-line no-console
  console.groupEnd(action.type); // eslint-disable-line no-console
  return result;
};

@krailler
Copy link

I have same issue; i have uninstall extension, and problem persist... replaceReducer still bugged.

@timdorr timdorr closed this as completed May 18, 2018
@greena13
Copy link

redux-devtools-instrument is waiting for this PR to be merged in.

@JoaoAlmeida-Kununu
Copy link

Still have this problem, and disabling the @redux-devtools/extension does not work for me.

@markerikson
Copy link
Contributor

@JoaoAlmeida-Kununu this issue was closed 6 years ago.

If you're having problems, please file a new issue with a reproduction or a Replay ( https://replay.io/record-bugs ).

@reduxjs reduxjs locked as resolved and limited conversation to collaborators Jul 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants