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.

Question: Enabling HRM Hot Module Replacement?

See original GitHub issue

Help us help you! Please choose one:

  • My app crashes with react-rails, so I’ve included the stack trace and the exact steps which make it crash.
  • My app doesn’t crash, but I’m getting unexpected behavior. So, I’ve described the unexpected behavior and suggested a new behavior.
  • I’m trying to use react-rails with another library, but I’m having trouble. I’ve described my JavaScript management setup (eg, Sprockets, Webpack…), how I’m trying to use this other library, and why it’s not working.
  • I have another issue to discuss.

Hi folks, I’m using Webpacker and react-rails and trying to get HRM Hot Module Replacement working on my project.

The webpack-dev-server.md doc in webpacker mentions that, in order to enable HMR for React we need add react-hot-loader as per these instructions.

I think I already did everything I need to do on the webpacker side of things: Step one, I did by adding the --hot option to the call of bin/webpack-dev-server in my Procfile. Additionally, I enabled the hmr setting in my webpacker.yml file.

I see in my console that HMR is enabled and it correctly listens to code changes. It’s unable to hot-load modules tho, because the modules are not accepted:

image

The instructions for react-hot-loader says to modify your root container to accept HMR by changing the root React entry point from something like this:

import React from 'react';
import { render } from 'react-dom';
import RootContainer from './containers/rootContainer.js';

render(<RootContainer />, document.getElementById('react-root'));

To something like this:

 if (module.hot) {
   module.hot.accept('./containers/rootContainer.js', () => {
     const NextRootContainer = require('./containers/rootContainer.js').default;
     render(<NextRootContainer />, document.getElementById('react-root'));
   })
 }

But, how do I do that with react-rails? I use the react_component Rails view helper to integrate React components into Rails views.

I’m suspecting that HMR is not successful in my project because the React code is not setup to accept the hot modules. Where in my code can I add

Any help would be greatly appreciated!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
edelgadocommented, Sep 29, 2017

Thanks for looking into it @BookOfGreg! Having HMR work with react-rails would be a huge time saver for developers as it would cut down on the amount of full-page refreshes we need to do when working on a component.

Here is an examples of HMR from from Dan himself: https://github.com/wkwiatek/react-hot-loader-minimal-boilerplate

More information can be found in http://gaearon.github.io/react-hot-loader/getstarted/

1reaction
edelgadocommented, Nov 7, 2019

As a follow-up, I was able to get HMR working with react-rails finally. It involves the use of the React Hot Loader NPM module and it’s babel plugin. Each component needs to be exported in a way that is HMR-friendly. Here is a sample Rails app with HMR working:

https://github.com/edelgado/react-rails-hmr

One downside is that HMR and Server-Side Rendering do not seem to be playing along: https://github.com/reactjs/react-rails/issues/925

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 — Hot Module Replacement - Medium
The easiest way to enable HMR is doing it inline. 1 .Enabling HRM : Inline method. All you need to do here is...
Read more >
A couple of questions about Webpack HMR and how to use it ...
These plugins provide a way to use hot module replacement (HMR) in your React application, but you will still need to add the...
Read more >
Webpack's Hot Module Replacement Feature Explained
Hot Module Replacement is a feature that enables you to see code changes in the browser without having to refresh it, allowing you...
Read more >
Hot Module Replacement - SurviveJS
Hot Module Replacement (HMR) builds on top of the WDS. It enables an interface that makes it possible to swap modules live. For...
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