Skip to content

Conversation

@EdmondChuiHW
Copy link

@EdmondChuiHW EdmondChuiHW commented Apr 26, 2020

requires #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: facebook/react#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

Comment on lines +9 to +10
var renderWithReactDOM = require("./src/renderComponent/withReactDOM")
var renderWithHotReload = require("./src/renderComponent/withHotReload")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the add render component extension point PR, the folder structure and import pattern is taking reference from getConstructor for consistency. Please let me know if there is a better way

Comment on lines +91 to +96
// Enables hot reload for component rendering.
//
// See the HMR section in README to ensure required steps are completed.
useHotReload: function(requireContext) {
this.renderComponent = renderWithHotReload(requireContext)
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this comment could be more helpful, i.e. should the steps to setup be inlined here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, there is an opportunity to "stack" instead of "replace". See the example and discussion at #1064. The proposed approach here mirrors getConstructor and useContext. Let me know if the "stack" option would be a better option

Comment on lines +5 to +7
// Render React component with hot reload.
//
// See the HMR section in README to ensure required steps are completed.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one. Let me know if this comment could be more helpful, i.e. should the steps to setup be inlined here?

@EdmondChuiHW EdmondChuiHW marked this pull request as ready for review April 26, 2020 00:48
@EdmondChuiHW EdmondChuiHW changed the title add hot reload support add auto hot reload support Apr 26, 2020
Comment on lines +687 to +717
1. install [react-hot-loader](https://github.com/gaearon/react-hot-loader) and [@hot-loader/react-dom](https://github.com/hot-loader/react-dom)
2. add the following to your webpack config in dev:
```js
{
module: {
rules: [
{
test: /\.(jsx|tsx)?$/,
use: ["react-hot-loader/webpack"],
},
],
},
resolve: {
alias: {
"react-dom": "@hot-loader/react-dom",
},
},
}
```
3. in your entry file, usually where you call `ReactRailsUJS.useContext` already, call `useHotReload`:
```js
var ReactRailsUJS = require("react_ujs")
var myCustomContext = require.context("custom_components", true)
ReactRailsUJS.useHotReload(myCustomContext)
```
4. optionally, for CSS to hot reload, update the following for dev in `webpacker.yml`:
```yml
development:
<<: *default
extract_css: false
```
Copy link
Author

@EdmondChuiHW EdmondChuiHW Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if the steps in readme is concise and simple to follow. Happy to pull into a separate file if requested

@BookOfGreg
Copy link
Member

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.

@danvernon
Copy link

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

@EdmondChuiHW
Copy link
Author

@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

@danvernon
Copy link

@BookOfGreg anything stopping this being merged?

@justin808
Copy link
Collaborator

justin808 commented Aug 15, 2022

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 justin808 closed this Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants