react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

add auto hot reload support

Open EdmondChuiHW opened this issue 4 years ago • 4 comments

requires https://github.com/reactjs/react-rails/pull/1064 to be merged

Summary

Automatically hot reload all components rendered with react_ujs without source file changes. Existing support requires manually wrapping all components with hot(App). This PR removes the need.

Other Information

Inline comments have been added to discuss options considered and their tradeoffs.

related: https://github.com/facebook/react/issues/16604

Also uncertain on how to fix the Travis CI build error

Steps to enable (also updated in README)

  1. install react-hot-loader and @hot-loader/react-dom
  2. add the following to your webpack config in dev:
{
  module: {
    rules: [
      {
        test: /\.(jsx|tsx)?$/,
        use: ["react-hot-loader/webpack"],
      },
    ],
  },
  resolve: {
    alias: {
      "react-dom": "@hot-loader/react-dom",
    },
  },
}
  1. in your entry file, usually where you call ReactRailsUJS.useContext already, call useHotReload:
var ReactRailsUJS = require("react_ujs")
var myCustomContext = require.context("custom_components", true)
ReactRailsUJS.useHotReload(myCustomContext)
  1. optionally, for CSS to hot reload, update the following for dev in webpacker.yml:
development:
  <<: *default
  extract_css: false

EdmondChuiHW avatar Apr 26 '20 00:04 EdmondChuiHW

I'll need to test it doesn't negatively affect any existing integration but this looks promising so thank you for contributing! The documentation also reads well to me.

Travis succeeded on all builds except Webpack 4 so I'll need to take a look at that.

BookOfGreg avatar May 12 '20 07:05 BookOfGreg

@EdmondChuiHW whats the status of this PR? would love to have access to this.

danvernon avatar Nov 10 '20 23:11 danvernon

@EdmondChuiHW whats the status of this PR? would love to have access to this.

@danvernon looks good to me 😜 Ask the author to see if there's anything I can do to help merge this

EdmondChuiHW avatar Nov 10 '20 23:11 EdmondChuiHW

@BookOfGreg anything stopping this being merged?

danvernon avatar Nov 11 '20 09:11 danvernon

We need a solution that uses: https://github.com/pmmmwh/react-refresh-webpack-plugin/

Per https://github.com/gaearon/react-hot-loader, react-hot-loader is no longer recommended.

The new plugin is much less intrusive to your source code. It does not require changing the source code, only the development build configuration to use webpack and babel plugins.

Shakapacker supports HMR with React:

https://github.com/shakacode/shakapacker/blob/master/docs/react.md#enabling-hot-module-replacement-hmr

justin808 avatar Aug 15 '22 23:08 justin808