最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I enable Hot Module Replacement(HMR) in react? - Stack Overflow

programmeradmin3浏览0评论

Does the create-react-app e with built-in support for HMR? I have seen react app reloads on changes. But that's not HMR. What webpack configuration I need to add to enable HMR. I read online about setting hot option in webpack-dev-serve to true. I'm confused about HMR in react.

After searching for a while I came across this

import React from 'react'
import ReactDOM from 'react-dom'
import App from './ponents/App'

// Opt-in to Webpack hot module replacement
if (module.hot) module.hot.accept()

ReactDOM.render(
  <App />,
  document.getElementById('app')
)

Then it says to add HotModuleReplacementPlugin in webpack.config.js
Do I need to manually add it or it es pre-added. Also I read that react uses ReactRefreshWebpackPlugin. I'm confused please help.

Does the create-react-app e with built-in support for HMR? I have seen react app reloads on changes. But that's not HMR. What webpack configuration I need to add to enable HMR. I read online about setting hot option in webpack-dev-serve to true. I'm confused about HMR in react.

After searching for a while I came across this

import React from 'react'
import ReactDOM from 'react-dom'
import App from './ponents/App'

// Opt-in to Webpack hot module replacement
if (module.hot) module.hot.accept()

ReactDOM.render(
  <App />,
  document.getElementById('app')
)

Then it says to add HotModuleReplacementPlugin in webpack.config.js
Do I need to manually add it or it es pre-added. Also I read that react uses ReactRefreshWebpackPlugin. I'm confused please help.

Share Improve this question asked Jun 11, 2021 at 7:06 Tarun SinghTarun Singh 4903 gold badges9 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First, we have to remember how HMR works:

It allows all kinds of modules to be updated at runtime without the need for a full refresh.

Second, we have to remember how React works: It only updates the parts of the DOM that need to be updated. For instance, if you have a React app and you add a paragraph to it, a DOM node for the paragraph will be added and the page won't reload. If you change the contents of an existing paragraph, only the DOM node that represents the contents of the paragraph will be reloaded, because it needs to be re-written.

Third,

Since webpack-dev-server v4.0.0, Hot Module Replacement is enabled by default.

Fourth,

Create React App uses webpack underneath the hood. You can see the entire configuration by ejecting it through the following cammand:

npm run eject

Putting it all together, Create React App does e with HMR enabled by default. You can confirm that by following the steps bellow:

  1. Create and run an app by executing these mands:
    npx create-react-app my-app
    cd my-app
    npm start
  1. After the app opens in your browser, select a piece of text, for instance, the word "reload". Then, go to your source code and change the background color of the application in the App.js file. You'll notice that the background color changes without a full refresh, since the text remains selected. Now, if you change the contents of the paragraph in your source code, you'll see that the selection disappears. This is React taking place an re-writing the DOM, only the small piece that needs to be updated. Furthermore, if select the text of the paragraph in your browser and go to your source code and change the contents of the link, you'll see that the content of the link is updated without clearing the selection, again, because React will only update the pieces of the DOM that need to be updated.
发布评论

评论列表(0)

  1. 暂无评论