使用webpack配置react-hot-loader熱加載局部更新
webpack配置react-hot-loader熱加載局部更新
有人會問 webpack-dev-server 已經(jīng)是熱加載了,能做到只要代碼修改了頁面也自動更新了,為什么在 react 項目還要安裝 react-hot-loader 呢?
其實這兩者的更新是有區(qū)別的,webpack-dev-server 的熱加載是開發(fā)人員修改了代碼,代碼經(jīng)過打包,重新刷新了整個頁面。
而 react-hot-loader 不會刷新整個頁面,它只替換了修改的代碼,做到了頁面的局部刷新。
但它需要依賴 webpack 的 HotModuleReplacement 熱加載插件。
下面來說說怎么來配置 react-hot-loader 。
步驟1
安裝 react-hot-loader
npm install --save-dev react-hot-loader
步驟2
在 webpack.config.js 的 entry 值里加上 react-hot-loader/patch,一定要寫在entry 的最前面,如果有 babel-polyfill 就寫在
babel-polyfill 的后面。
entry: [ 'babel-polyfill', 'react-hot-loader/patch', //設(shè)置這里 __dirname + '/app/main.js' ]
步驟3
在 webpack.config.js 中設(shè)置 devServer 的 hot 為 true
devServer: { contentBase: './build', port: '1188', historyApiFallback: true, inline: true, hot: true, //設(shè)置這里 },
步驟4
在 .babelrc 里添加 plugin
{ "presets": ['es2015', 'react'], "plugins": ["react-hot-loader/babel"] //設(shè)置這里 }
步驟5
在 webpack.config.js 的 plugins 里添加依賴的 HotModuleReplacement 插件
plugins: [ new HtmlWebpackPlugin({ template: __dirname + "/app/index.tmpl.html" }), new webpack.HotModuleReplacementPlugin() //設(shè)置這里 ]
步驟6
最后這個操作就是在項目的主入口,比如我的是 main.js 添加些代碼
import React from 'react'; import ReactDOM from 'react-dom'; import Greeter from './greeter'; import "./main.css"; import { AppContainer } from 'react-hot-loader'; //設(shè)置這里 const render = (App) => { ReactDOM.render( <AppContainer> <App /> </AppContainer>, document.getElementById('root') ) } render(Greeter) // Hot Module Replacement API if (module && module.hot) { module.hot.accept('./greeter', () => { render(require('./greeter').default) }) }
簡寫成這樣試了一下也能成功
import React from 'react'; import ReactDOM from 'react-dom'; import App from './app'; import "./main.css"; ReactDOM.render( <App />, document.getElementById('root') ) if (module && module.hot) { module.hot.accept() }
按順序做完上面6個步驟,恭喜你,React 的 react-hot-loader 已經(jīng)配置好了。
react-hot-loader熱加載不生效的可能問題
如果是下面這樣,就不會執(zhí)行熱加載
const AsyncLogin = asyncComponent(() => import("./Login")); //我不能熱加載 import home from './home' ?//我能熱加載 <Route exact path='/' component={home}/> <Route ? path='/login' component={AsyncLogin}/> <Route ? path='/home' component={home}/>
也就是說需要同步引入組件才可以局部熱加載成功
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在React中使用React.createRef:更優(yōu)雅的DOM引用方式
React提供了多種方式來引用DOM元素,其中React.createRef()是一種更為現(xiàn)代、更優(yōu)雅的方式,在這篇文章中,我們將深入了解React.createRef()的應(yīng)用,以及它為開發(fā)者帶來的便利,感興趣的朋友一起看看吧2024-01-01使用 React hooks 實現(xiàn)類所有生命周期
react 自 16.8 開始,引入了 Hooks 概念,使得函數(shù)組件中也可以擁有自己的狀態(tài),并且可以模擬對應(yīng)的生命周期,這篇文章主要介紹了使用 React hooks 怎么實現(xiàn)類里面的所有生命周期,需要的朋友可以參考下2023-02-02React實現(xiàn)文件上傳和斷點續(xù)傳功能的示例代碼
這篇文章主要為大家詳細介紹了React實現(xiàn)文件上傳和斷點續(xù)傳功能的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02詳解react-router 4.0 下服務(wù)器如何配合BrowserRouter
這篇文章主要介紹了詳解react-router 4.0 下服務(wù)器如何配合BrowserRouter,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12React 數(shù)據(jù)獲取與性能優(yōu)化詳解
這篇文章主要為大家介紹了React 數(shù)據(jù)獲取與性能優(yōu)化方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10