詳解升級(jí)react-router 4 踩坑指南
一.前言
上午把近日用React做的一個(gè)新聞項(xiàng)目所依賴的包升級(jí)到了最新的版本,其中從react-router(2.8.1)升級(jí)到react-router(4.1.2)中出現(xiàn)了很多問(wèn)題, 故總結(jié)一下在升級(jí)過(guò)程中遇到的問(wèn)題.
二.react-router,V4版本修改內(nèi)容
1. 所有組件更改為從react-router-dom導(dǎo)入
之前的所有路由組件均是從react-router中導(dǎo)入,在我之前的項(xiàng)目中,導(dǎo)入相關(guān)組件如下:
//v2 import {Router,Route,hashHistory} from 'react-router';
在react-router4 開(kāi)始,所有的router組件均是從react-router-dom中導(dǎo)入, 所以一下的需要更改為以下代碼:
//v4 import {Route,BrowserRouter, Switch} from 'react-router-dom';
細(xì)心的你發(fā)現(xiàn)在,到導(dǎo)入的過(guò)程中,我刪除了Router,但是增加了BorwerRouter和Switch,下面我會(huì)一步步的說(shuō)明.
2. 將所有<Router>替換為<BrowserRouter>
之前v2中的代碼如下:
//v2 <Router history={hashHistory}> <Route path="/" component={PCIndex}></Route> <Route path="/details/:uniqueky" component={PCNewsDetails}></Route> <Route path="/usercenter" component={PCUserCenter}></Route> </Router>
現(xiàn)在需要更改為BrowserRouter
//v4 <BrowserRouter> <Switch> <Route exact path="/" component={MobileIndex}></Route> <Route path="/details/:uniqueky" component={MobileNewsDetails}></Route> <Route path="/usercenter" component={MobileUserCenter}></Route> </Switch> </BrowserRouter>
細(xì)心的你發(fā)現(xiàn),這里的代碼不僅僅是將Router替換為BrowserRouter,而且還把所有的Route中用Switch包裹起來(lái). 下面就是v4的另一個(gè)修改.
3. <BrowserRouter>只能有一個(gè)子節(jié)點(diǎn)
<BroserRouter>只能有一個(gè)子節(jié)點(diǎn),所以官網(wǎng)建議的是使用<Switch>進(jìn)行包裹,官網(wǎng)給出的例子如下.
In v3, you could specify a number of child routes, and only the first one that matched would be rendered.
// v3 <Route path='/' component={App}> <IndexRoute component={Home} /> <Route path='about' component={About} /> <Route path='contact' component={Contact} /> </Route>
v4 provides a similar functionality with the <Switch> component. When a <Switch> is rendered, it will only render the first child <Route> that matches the current location.
// v4 const App = () => ( <Switch> <Route exact path='/' component={Home} /> <Route path='/about' component={About} /> <Route path='/contact' component={Contact} /> </Switch> )
4. 最坑的地方:在當(dāng)前目錄下的文件路徑不再使用./, 而是直接用/.
在進(jìn)行文件引用的時(shí)候 ,./src/js的寫法需要更改文'/src/js', 這是更改之后最坑的地方!!! 因?yàn)槠渌母?在控制臺(tái)都會(huì)有著詳細(xì)的錯(cuò)誤提示, 然而找不到文件只會(huì)出現(xiàn)404!!!
所以,在單頁(yè)面中的引入的css文件和bundle.js的引入都需要更改, 在我的項(xiàng)目中的例子如下:
//v2 <!DOCTYPE html> <html lang="en"> <head> <title></title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="./src/css/pc.css" rel="external nofollow" > <link rel="stylesheet" href="./src/css/mobile.css" rel="external nofollow" > </head> <body> <div id="mainContainer"> </div> <script src="./src/bundle.js"></script> </body> </html>
上面的頁(yè)面需要更改為下面這樣,否則所有的文件都無(wú)法找到:
//v4 <!DOCTYPE html> <html lang="en"> <head> <title></title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="/src/css/pc.css" rel="external nofollow" > <link rel="stylesheet" href="/src/css/mobile.css" rel="external nofollow" > </head> <body> <div id="mainContainer"> </div> <script src="/src/bundle.js"></script> </body> </html>
三.更多信息
以上就是我在我的項(xiàng)目中所遇到的坑,以及對(duì)應(yīng)的處理辦法.總的來(lái)說(shuō)react-router4 rewrite之后變化還是挺大的.
1. 更多React-router v4的修改信息,請(qǐng)看Github:
2. 本文中的項(xiàng)目下載地址:
Github: https://github.com/Lee-Tanghui/React-news-project.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解
這篇文章主要為大家介紹了React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10React實(shí)現(xiàn)Vue的watch監(jiān)聽(tīng)屬性方式
這篇文章主要介紹了React實(shí)現(xiàn)Vue的watch監(jiān)聽(tīng)屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01使用react context 實(shí)現(xiàn)vue插槽slot功能
這篇文章主要介紹了使用react context 實(shí)現(xiàn)vue插槽slot功能,文中給大家介紹了vue的slot的實(shí)現(xiàn)方法,需要的朋友可以參考下2019-07-07詳解React??App.js?文件的結(jié)構(gòu)和作用
在React應(yīng)用中,App.js文件通常是項(xiàng)目的根組件文件,它負(fù)責(zé)組織和渲染其他組件,是應(yīng)用的核心部分,本文將詳細(xì)介紹App.js文件的結(jié)構(gòu)、作用和最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-08-08React錯(cuò)誤邊界Error Boundaries
錯(cuò)誤邊界是一種React組件,這種組件可以捕獲發(fā)生在其子組件樹(shù)任何位置的JavaScript錯(cuò)誤,并打印這些錯(cuò)誤,同時(shí)展示降級(jí)UI,而并不會(huì)渲染那些發(fā)生崩潰的子組件樹(shù)2023-01-01react18?hooks自定義移動(dòng)端Popup彈窗組件RcPop
這篇文章主要介紹了react18?hooks自定義移動(dòng)端Popup彈窗組件RcPop,react-popup?基于react18+hook自定義多功能彈框組件,整合了msg/alert/dialog/toast及android/ios彈窗效果,需要的朋友可以參考下2023-08-08