CRA v1 + Hot Module Reload (HMR) + Redux
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:29
- Comments:17 (2 by maintainers)
Top GitHub Comments
With Webpack 2 (which CRA now uses) this should be enough:
It’s because
App
is live-bound to ES6 import in Webpack 2.Same with the other example:
@gnapse @bfncs +1 - was also able to use a pure functional
<App />
.