react-hot-loader: Hot Reload doesn't work when starting inside a function

If you are reporting a bug or having an issue setting up React Hot Loader, please fill in below. For feature requests, feel free to remove this template entirely.

Description

For the next branch, the hot reload does not work if the component being rendered is started inside a function.

Expected behavior

Being inside a function should not make any difference.

Actual behavior

It only works if the component being rendered is run at the top level. Meaning, the top level file that calls ReactDOM.render has to run directly, ReactDOM.render cannot be run inside a function that mounts the component which has been “hotted.”

Environment

React Hot Loader version: next, ^4.0.0-beta.12

Run these commands in the project folder and fill in their results:

  1. node -v: v8.9.0
  2. npm -v: 5.5.1

Then, specify:

  1. Operating system: OS X 10.12.4
  2. Browser and version: Chrome 63

Reproducible Demo

Doesn’t work

https://gist.github.com/chrisabrams/e0fe9cf8ea7347fe2364cc46cf378412

Does work

https://gist.github.com/chrisabrams/293a08cfb98b6d8f0bb5aff4464f5874

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18

Commits related to this issue

Most upvoted comments

Any example of how to make it work with redux connect() ?


Example:
export default connect(mapStateToProps)(App)

Only App.js and Index.js is hot-reloading rest doesnot.

@theKashey What JS error? First example works just fine for me. The issue I have is that I don’t know what the component is going to be, so I need to put it inside a function. I can’t export default it directly as it might be wrapped by another component. I was hoping that I could do something such as:

export default function renderHot(Component) {

    return hot(module)(Component)

}

which would make it hot on the fly. That does not work though. To be more clear:

This works:

import React from 'react'
import { hot } from 'react-hot-loader'

const AppC = () => <div>Hello World!! 10</div>
const HotApp = hot(module)(AppC)

export default function App() {

  return HotApp

}

This does not work:

import React from 'react'
import { hot } from 'react-hot-loader'

const AppC = () => <div>Hello World!! 10</div>

export default function App() {

  return hot(module)(AppC)

}

What specifically makes hot being called inside a function cause [HMR] The following modules couldn't be hot updated: (Full reload needed)?