-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Question: differences between Webpack HMR & React-Hot-Loader ? #1063
Comments
If you want to fully understand the topic in depth I wrote something about it. Webpack HMR is the underlying mechanism for replacing JS modules on the fly. It offers an API for doing so ( HMR API is available in Create React App. It means you can write code like this: a.jsconst b = require('./b');
alert('At first b is ' + b);
if (module.hot) {
module.hot.accept('./b', function() {
const updatedB = require('./b');
alert('But now it is ' + updatedB);
});
} b.jsmodule.exports = 42; // edit to make it 43 This is all HMR API gives you: an ability for the app to handle "new versions" of the same module and do something with it. React Hot Loader is a project that attempts to build on top of Webpack HMR and preserve DOM and React component state when components are saved. Normally it's hard to achieve with vanilla Webpack HMR because if you just replace a module, React will think it's a different component type and destroy DOM and local state. So React Hot Loader does many tricks attempting to prevent this, but it's also way more complex than underlying HMR API.
This is not "HMR". This is just a refresh. "HMR" is what you see in CSS: when you change CSS file, it updates without refreshing. This is because The reason you don't get HMR behavior even though it's enabled in Create React App is because you haven't written any Of course manually adding
You can get reasonably close with this approach. However, unlike React Hot Loader, it wouldn't preserve the component local state or DOM, so it's just a "faster reload". There is no way to fully have RHL-like experience in CRA. I hope to gradually get there but it's a longer road. See gaearon/react-hot-boilerplate#97 (comment) for details. I hope this helps! |
Thanks for the deep explanations, they were a long read but were what i was searching for, thanks ! FOr now, i'll just follow your advice to activate Webpack HMR only till you or someone else gets this to work, quoting the 4th point of your plan:
Speaking about folowing the adviced approach linked in your answer, it works for a barebone React Project but... real projects uses a router, an internationalization, and CSS framework, most of the time i think. So for instance, my project's index.js looks like that:
... it doesnot change anything, still full page reload. Btw it takes about 2secs to recompile, then 2 other for the browser to refresh. That's why i was really astonished when seeing your video with instant change refresh. What am i doing wrong ? PS: my App component is actually the base one in routes.js |
You likely want something like this? import React from 'react'
import { render } from 'react-dom'
import Provider from 'react-redux/lib/components/Provider'
import Router from 'react-router/lib/Router'
import browserHistory from 'react-router/lib/browserHistory'
// Note: I can't see where you defined them so I added this:
import routes from './routes';
const rootEl = document.getElementById('root')
render(
<Provider store={store}>
<Router history={browserHistory} routes={routes} />
</Provider>,
rootEl
)
if (module.hot) {
// This is not a magic thing!
// You need to put whatever changes in *your* project here.
module.hot.accept('./routes', () => {
const nextRoutes = require('./routes').default // Again, depends on your project
render(
<Provider store={store}>
<Router history={browserHistory} routes={nextRoutes} />
</Provider>,
rootEl
)
})
} As I explained above |
Still 4secs full page reload :s I'm wondering if i forgot to install something else besides CRA itself ? Here 's an example of my routes.js
...and the HelloPage is a dumb Component
If i modify "Hello" with "Yop" it doesnot "autoreflect" in browser, but if i save it triggers a full page reload. I've read in your long article ReactTransform and RHL had troubles detecting components not using class extends Component or React.createClass(), so i tried also with HelloPage with class: still same behavior :s I also tried only with just "npm start" from Windows CLI and modify in SublimeText (instead of Webstorm, thinking maybe something in the IDE would prevent autorefresh) but still same behavior. Did i understood correctly when i read adding module.hot condition in topmost component in the tree would be enugh to catch any change bubbling up ? PS: forgot to mention CRA ask me if i want to change port on start, since port3000 is already used by my API, not sure if this could have an impact on the problem ? |
It's hard to say anything without an example reproducing your problem. |
Ok :
expected results :It displays the expected black Header with spinning React logo HMR attempt :Now i uncomment in index.js the hmr condition and save (it recompiles) It should NOT refresh the unmodified components, ie Header, logo, etc... => fail |
@Sharlaan Typo in your code:
|
@Sharlaan I tried @gaearon I can't correctly configure a PERFECTLY |
@cpunion Please direct this feedback to React Hot Loader repository. Yes, there is an example, and it's linked for React Hot Loader README's first paragraph. gaearon/react-hot-loader#240 |
I’m locking this particular thread because it’s not related to Create React App at the moment, and comments will just get lost unaddressed here. Please file issues with the projects you are using for hot reloading. |
I'm looking forward integration of webpack2 "and" React-Hot-Loader 3 (RHL) in CRA...
... but then i realized i'm not sure to fully understand the differences between webpack's HMR and RHL:
Are they doing same thing ? Are they mutually exclusive, or on contrary they must be used together to prevent full page refresh ?
At the moment, i'm using current CRA which seems to only use HMR: during dev, a change of a simple text or inner component, auto-refreshes the whole page.
Fine but when i saw this RHL video, i was like WoW it's awesome... but then why my CRA (which includes built-in "HMR") doesnot behave like that o.O ?!
I'm kinda confused, could someone explain the differences to get similar dev experience with CRA like in RHL video please ?
P.S.: not sure if that is pertinent, i'm using Webstorm 2016.3
The text was updated successfully, but these errors were encountered: