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.

Description

HMR does not work.

Expected behavior

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import './index.css';

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

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

Hot updates should refresh page.

Actual behavior

Nothing happens.

see https://github.com/webpack/webpack-dev-server/issues/206

https://github.com/facebookincubator/create-react-app/blob/88c15d0988fcb456fc976cbe15823ed518470126/packages/react-scripts/scripts/start.js#L221 uses hot option and https://github.com/facebookincubator/create-react-app/blob/88c15d0988fcb456fc976cbe15823ed518470126/packages/react-scripts/config/webpack.config.dev.js#L206 uses the plugin.

Removing either solved it for me.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
gaearoncommented, Oct 12, 2016

Removing either solved it for me.

I don’t think it “solved” the problem for you, it just refreshed the page.

Your example doesn’t work because it is written for Webpack 2. We still use Webpack 1. Here’s the version that works with Webpack 1:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import './index.css';

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

if (module.hot) {
  module.hot.accept('./components/App', () => {
    var NextApp = require('./components/App').default;
    ReactDOM.render(
      <NextApp />,
      document.getElementById('root')
    );
  });
}

Note that this resets internal state of components. Create React App doesn’t currently support preserving component state on hot reloading.

0reactions
Timercommented, Apr 5, 2017

The examples you see are still the recommended way to do this.

However, no stable release depends on webpack 2. Please use the webpack 1 example for now. It should continue to work when we switch to 2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hot Module Replacement - webpack
Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up ......
Read more >
Webpack's Hot Module Replacement Feature Explained
HMR allows you to exchange, add, or remove JavaScript modules while the application is running, all without having to reload the browser. This ......
Read more >
What exactly is Hot Module Replacement in Webpack?
HMR is a way of exchanging modules in a running application (and adding/removing modules). You basically can update changed modules without a full...
Read more >
Hot Module Replacement - SurviveJS
Webpack exposes the HMR interface through a global variable: module.hot . It provides updates through module.hot.accept(<path to watch>, <handler>) function and ...
Read more >
Understanding webpack HMR beyond the docs - Jakob Lind
HMR (Hot Module Replacement) can give you a super smooth developer experience by automatically replace running code in the browser whenever you make...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

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