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.

Full reloads with react-hot-loader/webpack

See original GitHub issue

Description

I tried using react-hot-loader/webpack as described in the docs, because I’m not using babel in my setup. However, this way the hot reloading doesn’t work, it reloads the top level container losing the state of all nested components.

Expected behavior

It should work the same as in react-hot-loader/babel mode - components are swapped without losing state.

Actual behavior

All state of all the components is lost upon reloading.

Environment

React Hot Loader version: 3.0.0-beta7

Reproducible Demo

To reproduce:

git clone git@github.com:KidkArolis/react-hot-boilerplate.git
git checkout webpack-loader-issue
npm install
npm start
open http://localhost:3000
vim src/Counter.js # and save, the counter resets, but it shouldn't

This is all I changed in the default react-hot-boilerplate: https://github.com/KidkArolis/react-hot-boilerplate/commit/c72df249e3b06586b8ec7fddb95174386a398b73 image

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
KidkAroliscommented, Sep 24, 2017

I have diffed the bundle.js produced in both modes and here is a comparison of individual module:

react-hot-loader/babel

{
  /***/
  "./src/Layout.js":
  /***/
    (function(module, __webpack_exports__, __webpack_require__) {

    "use strict";
    /* harmony export (immutable) */
    __webpack_exports__["a"] = Layout;
    /* harmony import */
    var __WEBPACK_IMPORTED_MODULE_0_react__ = __webpack_require__("./node_modules/react/react.js");
    /* harmony import */
    var __WEBPACK_IMPORTED_MODULE_0_react___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_react__);


    function Layout(_ref) {
      var children = _ref.children;

      return __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement(
        'div',
        null,
        __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement(
          'h1',
          null,
          'Hello, world!'
        ),
        children
      );
    };

    var _temp = function() {
      if (typeof __REACT_HOT_LOADER__ === 'undefined') {
        return;
      }

      __REACT_HOT_LOADER__.register(Layout, 'Layout', '/Users/karolis/Desktop/react-hot-boilerplate/src/Layout.js');
    }();

    ;

    /***/
  })
}

react-hot-loader/webpack

{
  /***/
  "./src/Layout.js":
  /***/
    (function(module, __webpack_exports__, __webpack_require__) {

    "use strict";
    /* WEBPACK VAR INJECTION */
    (function(process, module) { /* harmony export (immutable) */
      __webpack_exports__["a"] = Layout;
      /* harmony import */
      var __WEBPACK_IMPORTED_MODULE_0_react__ = __webpack_require__("./node_modules/react/react.js");
      /* harmony import */
      var __WEBPACK_IMPORTED_MODULE_0_react___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_react__);


      function Layout(_ref) {
        var children = _ref.children;

        return __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement(
          'div',
          null,
          __WEBPACK_IMPORTED_MODULE_0_react___default.a.createElement(
            'h1',
            null,
            'Hello, world!'
          ),
          children
        );
      }

      ;
      (function register() { /* react-hot-loader/webpack */
        if (process.env.NODE_ENV !== 'production') {
          if (typeof __REACT_HOT_LOADER__ === 'undefined') {
            return;
          }
          if (typeof module.exports === 'function') {
            __REACT_HOT_LOADER__.register(module.exports, 'module.exports', "/Users/karolis/Desktop/react-hot-boilerplate/src/Layout.js");
            return;
          }
          for (var key in module.exports) {
            if (!Object.prototype.hasOwnProperty.call(module.exports, key)) {
              continue;
            }
            var namedExport = void 0;
            try {
              namedExport = module.exports[key];
            } catch (err) {
              continue;
            }
            __REACT_HOT_LOADER__.register(namedExport, key, "/Users/karolis/Desktop/react-hot-boilerplate/src/Layout.js");
          }
        }
      })();
      /* WEBPACK VAR INJECTION */
    }.call(__webpack_exports__, __webpack_require__("./node_modules/node-libs-browser/node_modules/process/browser.js"), __webpack_require__("./node_modules/webpack/buildin/harmony-module.js")(module)))

    /***/
  })
}
0reactions
gregbergecommented, Oct 10, 2017

About this issue, this is probably due to Webpack since you see a full reload. Also, Webpack plugin support is very limited (only module exports).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started · React Hot Loader - Dan gaearon
React Hot Loader is a plugin that allows React components to be live reloaded without the loss of state. It works with Webpack...
Read more >
GitHub - gaearon/react-hot-loader
GitHub - gaearon/react-hot-loader: Tweak React components in real time. ... Watch Dan Abramov's talk on Hot Reloading with Time Travel.
Read more >
Webpack/react hot reload reloading whole page?
It works, and when I change a file (say, src/components/Note/Note.css ) the app does reload. However, I want to only reload the component,...
Read more >
Set up React Hot Loader in 10 minutes - LogRocket Blog
A hot reload to an app will only refresh the files that were changed without losing the application's state. · A live reload...
Read more >
Enabling hot reload in React web app | by Dong Chen - Medium
Hot reload allows developers to see result of code change in browser without page refresh. This greatly improves developer productivity and experience, ...
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