question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CRA v1 + Hot Module Reload (HMR) + Redux

See original GitHub issue

I’d like to confirm the ‘correct’ way to have CRA + HMR + React now we are on version 1 and using webpack 2, and that all these thoughts are correct. They may be useful to others adding HMR.

Examples have been updated incorporating feedback

Why Hot Module Reload

Adding in HMR changes your application from full page refresh to refreshing just the app. In most cases this will make the page reload faster.

If you are using external state management such as Redux, you can have your redux state remain when component changes are made.

Note: This is not same a component based hot-module-reloading where state within your react application remains unchanged when making changes to your source code. HMR will remove any component-based state. That is currently unsupported by CRA, more information see react-hot-loader and status post by gaereon.

Hot Module Reload without Redux

index.js

// regular imports
ReactDOM.render(<App /> , document.getElementById('root'))

if (module.hot) {
  module.hot.accept('./App', () => {
    ReactDOM.render(<App />, document.getElementById('root'))
  })
}

As seen here and in issue #897

Hot Module Reload with Redux (or similar state management)

index.js

// Normal imports
import { Provider } from 'react-redux'
import configureStore from './redux/configureStore'

const store = configureStore()

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>
  , document.getElementById('root'))

if (module.hot) {
  module.hot.accept('./App', () => {
    ReactDOM.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root'),
    )
  })
}

configureStore.js (or similar)

import { createStore } from 'redux'

import rootReducer from './reducers'

const configureStore = () => {
  const store = createStore(rootReducer)

  if (process.env.NODE_ENV !== "production") {
    if (module.hot) {
      module.hot.accept('./reducers', () => {
        store.replaceReducer(rootReducer)
      })
    }
  }

  return store
}

export default configureStore

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:29
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

80reactions
gaearoncommented, May 22, 2017

With Webpack 2 (which CRA now uses) this should be enough:

// regular imports
ReactDOM.render(<App /> , document.getElementById('root'))

if (module.hot) {
  module.hot.accept('./App', () => {
    ReactDOM.render(<App />, document.getElementById('root'))
  })
}

It’s because App is live-bound to ES6 import in Webpack 2.

Same with the other example:

import { createStore } from 'redux'

import rootReducer from './reducers'

const configureStore = () => {
  const store = createStore(rootReducer)

  if (process.env.NODE_ENV !== "production") {
    if (module.hot) {
      module.hot.accept('./reducers', () => {
        store.replaceReducer(rootReducer)
      })
    }
  }

  return store
}

export default configureStore
3reactions
onpawscommented, Oct 6, 2017

@gnapse @bfncs +1 - was also able to use a pure functional <App />.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hot Module Replacement in Redux - Toptal
This is a minimal example of hot module replacement (or HMR) in a Redux application. The working demo code is hosted on GitHub....
Read more >
How To Enable Hmr When Cra2 And Redux Are Used? - ADocLib
I'd like to confirm the 'correct' way to have CRA + HMR + React now we are on version 1 and using webpack...
Read more >
How to enable HMR when CRA2 and redux are used?
I'm developing a React app, created with create-react-app v2. Recently I began to use a CSS-in-JS library, namely, styled-components. Beforehand ...
Read more >
React Fast Refresh — The New React Hot Reloader
config.js module.exports = function babel(api) { const BABEL_ENV = api.env(); const presets = [...]; const plugins = [ ...
Read more >
Hot Module Replacement - webpack
This guide extends on code examples found in the Development guide. Hot Module Replacement (or HMR) is one of the most useful features...
Read more >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found