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.

react-refresh + ReactDOM: hot reloading only works when bundling React

See original GitHub issue

Do you want to request a feature or report a bug?

bug?

What is the current behavior?

note: I am encountering this issue when using https://github.com/pmmmwh/react-refresh-webpack-plugin, but I believe it’s an issue with react-refresh itself.

The react-refresh runtime overrides __REACT_DEVTOOLS_GLOBAL_HOOK__.inject to get a reference to the React renderer. In my app, the inject method is never called however, because I load react/react-dom from a third-party CDN before my application code. This means that changed components are never actually refreshed in the DOM.

I believe the issue is that scripts are loaded in this order:

  1. react-devtools sets up the global hook
  2. react/react-dom are loaded on the page, and inject() is called
  3. user code (which is instrumented with the react-refresh babel plugin) is loaded on the page, and inject() is monkey-patched

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

Follow the usage steps in the https://github.com/pmmmwh/react-refresh-webpack-plugin repo, but also add react and react-dom as externals in your Webpack build:

module.exports = {
  //...
  externals: {
    react: 'React',
    'react-dom': 'ReactDOM'
  }
};

then load those scripts onto the page from a CDN (e.g. unpkg or cdnjs) before the Webpack bundle.

What is the expected behavior?

react-refresh works the same whether React/ReactDOM are bundled with application code, or loaded via an external script.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.8.6 / Chrome / osx

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
benjamingrcommented, Dec 23, 2021

I have a weird ask - is there a way for react-refresh to work with a production version of React? (and load after)

Our current workaround (as a plugin hosted in another site) is to use a browser extension to swap the react production build for react development build but that’s not really optimal.

0reactions
gaearoncommented, Dec 17, 2019

Fixed in react-refresh@0.7.1. You’ll need a development version of React and >= 16.10 or so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hot Reload is not working in my React App
To solve the problem in hot reloading/fast_refresh I simply add CHOKIDAR_USEPOLLING=true in package.json:
Read more >
Enabling hot reload in React web app | by Dong Chen - Medium
Enabling hot reload in React web app. Hot reload allows developers to see result of code change in browser without page refresh. This...
Read more >
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 >
Hot Reload a React App in Development with ...
Generating new bundles when we change files is a great start, and the live-reload provided by webpack dev server is convenient, but a...
Read more >
Set up a React project with Typescript, Hot reload and Webpack
Once done, run the command: yarn build . Add index.html file. Our bundled file runs only on the node, and we want it...
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