天天看點

詳解更新react-router 4 踩坑指南

一.前言

上午把近日用React做的一個新聞項目所依賴的包更新到了最新的版本,其中從react-router(2.8.1)更新到react-router(4.1.2)中出現了很多問題, 故總結一下在更新過程中遇到的問題.

二.react-router,V4版本修改内容

1. 所有元件更改為從 react-router-dom 導入

之前的所有路由元件均是從 react-router 中導入,在我之前的項目中,導入相關元件如下:
//v2
import {Router,Route,hashHistory} from 'react-router';
                
在react-router4 開始,所有的router元件均是從 react-router-dom 中導入, 是以一下的需要更改為以下代碼:
//v4
import {Route,BrowserRouter, Switch} from 'react-router-dom';
                
細心的你發現在,到導入的過程中,我删除了 Router ,但是增加了 BorwerRouter 和 Switch ,下面我會一步步的說明.

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>
                
現在需要更改為 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>
                
細心的你發現,這裡的代碼不僅僅是将 Router 替換為 BrowserRouter ,而且還把所有的 Route 中用 Switch 包裹起來. 下面就是v4的另一個修改.

3. <BrowserRouter> 隻能有一個子節點

<BroserRouter> 隻能有一個子節點,是以官網建議的是使用 <Switch> 進行包裹,官網給出的例子如下.
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. 最坑的地方:在目前目錄下的檔案路徑不再使用 ./ , 而是直接用 / .

在進行檔案引用的時候 , ./src/js 的寫法需要更改文'/src/js', 這是更改之後最坑的地方!!! 因為其他的更改,在控制台都會有着詳細的錯誤提示, 然而找不到檔案隻會出現 404 !!!我真的找了好久的原因!!! 記住!!! 在單頁面中的引入的 css 檔案和 bundle.js 的引入都需要更改, 在我的項目中的例子如下:
//v2
<!DOCTYPE html>
<html >
    <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" target="_blank" rel="external nofollow" >
        <link rel="stylesheet" href="./src/css/mobile.css" target="_blank" rel="external nofollow" >
    </head>
    <body>
        <div id="mainContainer">
            
        </div>
        <script src="./src/bundle.js"></script>
    </body>
</html>
                
上面的頁面需要更改為下面這樣,否則所有的檔案都無法找到:
//v4
<!DOCTYPE html>
<html >
    <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" target="_blank" rel="external nofollow" >
        <link rel="stylesheet" href="/src/css/mobile.css" target="_blank" rel="external nofollow" >
    </head>
    <body>
        <div id="mainContainer">
            
        </div>
        <script src="/src/bundle.js"></script>
    </body>
</html>
                

三.更多資訊

以上就是我在我的項目中所遇到的坑,以及對應的處理辦法.總的來說react-router4 rewrite之後變化還是挺大的. 1. 更多React-router v4的修改資訊,請看Github:
Migrating from v2/v3 to v4
2. 本文中的項目下載下傳位址:
Github: https://github.com/Lee-Tanghui/React-news-project.git
轉自:http://www.jianshu.com/p/1f0af7763d7c