-
-
Notifications
You must be signed in to change notification settings - Fork 27k
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
CRA v1 + Hot Module Reload (HMR) + Redux #2317
Comments
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 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 |
Really helpful, thanks a lot! Note, that the component you are hot reloading ( |
for some weird reason my state gets wiped clean on hot reloading. I'm using plain react, no redux |
Edit: suddenly it started working ... and I have no idea why ... |
@rmoorman would you be able to share a small repo where all the code is present? I've been trying to get this to work for a couple of hours now and can't succeed... I can see that the HMR picks up the change in the Any help would be awesome... |
@bfncs @gaearon Am I missing something here? index.js
Store.js
|
@nealoke here you go. Pretty basic. Whenever you change the app component (or the rootReducer), state is kept. |
Thinks 😜 |
thanks! these help a lot! |
@nealoke yes, it's working, the store can update when I change reducers. I use gaearon's approach. |
@bfncs I just got it to work with a pure functional component. Perhaps something has changed since you made that comment? |
I'm not sure what I'm doing wrong here... AFAICT, my But when I make changes to ... Not only do I not see the changes applied.. But now the page won't even reload. I have to manually reload now, to see said changes. Here's the content of my import React from 'react'
import ReactDOM from 'react-dom'
import './index.scss'
import App from './App'
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
import createStore, { history } from './store'
import registerServiceWorker from './registerServiceWorker'
import 'bootstrap/dist/css/bootstrap.min.css'
const store = createStore()
registerServiceWorker()
const rootElem = document.getElementById('root')
const AppJsx = <Provider store={store}>
<ConnectedRouter history={history}>
<div>
<App />
</div>
</ConnectedRouter>
</Provider>
ReactDOM.render(
AppJsx,
rootElem
)
if (module.hot) {
module.hot.accept('./App', () => {
ReactDOM.render(AppJsx, rootElem)
})
} import { createStore, applyMiddleware, compose } from 'redux'
import { connectRouter, routerMiddleware } from 'connected-react-router'
import thunk from 'redux-thunk'
import createHistory from 'history/createBrowserHistory'
import rootReducer from './modules'
export const history = createHistory()
const initialState = {}
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
export default () => createStore(
connectRouter(history)(rootReducer),
initialState,
composeEnhancer(
applyMiddleware(
thunk,
routerMiddleware(history),
),
),
) Any help would be greatly appreciated :-) |
@RavenHursT I think this blog post will help you here, I found it a big help today for getting HMR to work on my project. https://medium.com/@brianhan/hot-reloading-cra-without-eject-b54af352c642 Your missing hot module replacement for your redux store. Eg:
I would suggest to also split up your |
Cool! I'm gonna try that out! Thanks! |
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
As seen here and in issue #897
Hot Module Reload with Redux (or similar state management)
index.js
configureStore.js (or similar)
The text was updated successfully, but these errors were encountered: