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

Support HMR for reducers #495

Closed
kimkha opened this issue Mar 22, 2017 · 6 comments
Closed

Support HMR for reducers #495

kimkha opened this issue Mar 22, 2017 · 6 comments

Comments

@kimkha
Copy link
Contributor

kimkha commented Mar 22, 2017

When I try to enable HMR on my project, console log said:

<Provider> does not support changing `store` on the fly. It is most likely that you see this error because you updated to Redux 2.x and React Redux 2.x which no longer hot reload reducers automatically. See https://github.com/reactjs/react-redux/releases/tag/v2.0.0 for the migration instructions.

We need to apply these simple code to support HMR. Guide here: https://github.com/reactjs/react-redux/releases/tag/v2.0.0

@djhi
Copy link
Collaborator

djhi commented May 12, 2017

This won't work. As explained in the redux documentation:

import { createStore } from 'redux';
import rootReducer from '../reducers/index';

export default function configureStore(initialState) {
  const store = createStore(rootReducer, initialState);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextRootReducer = require('../reducers/index');
      store.replaceReducer(nextRootReducer);
    });
  }

  return store;
}

Note the two lines:

  • module.hot.accept('../reducers', () => {
  • const nextRootReducer = require('../reducers/index');

My understanding is that we need to know the module path for hot reloading to work.

@fazo96
Copy link

fazo96 commented Feb 26, 2019

@fzaninotto this still happens on the latest version

@fzaninotto
Copy link
Member

React-admin doesn't impose the use of webpack, nor does it know where the reducers will end up in the final bundle.

So my understanding is that HMR support should be added in userland.

But maybe I misunderstood how that works?

@fazo96
Copy link

fazo96 commented Feb 28, 2019

@fzaninotto hot module reload can be enabled for React components, redux reducers and redux sagas.

For react components we can do it in userland by wrapping the Admin component, however this means the redux store gets remounted when the component is hot updated which breaks react-redux (they do not support this). I tried this, unfortunately on hot updates the Admin panel breaks.

The correct way to support hot module reload is to follow the steps in the documentation of react-hot-loader, react-redux and redux-saga to make sure the top level component, reducer and saga can accept hot updates.

Doing this for react-admin is only possible through forking and not in userland.

@djhi showed how to support it for react-redux. If module.hot is undefined because the environment is not webpack or does not support hot module reload, nothing happens.

This means that if the environment does not have hot module reload turned on, nothing happens. Even if not running in webpack, alternatives to it (like parcel) can take advantage of the hot module reload support so it will work there too, or if they don't support it then they won't break.

The only extra library needed for this functionality is the react-hot-loader wrapper that you are supposed to use with your top level component (but below <Provider>) to make it accept hot updates.

@fzaninotto
Copy link
Member

Thanks for the explanation. I'm reopening the enhancement request. A PR to implement the solution is welcome.

@mabhub
Copy link
Contributor

mabhub commented Sep 27, 2019

Shouldn't this enhancement request having been reopened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants