React Router V4 Recap and Demo

React Router V4 簡介

最近很久沒有React Router扫皱,好像上一次用還在用V3瑰抵,最近被問了個關(guān)于react routerv4的小問題,怎么也想不起來了止吐,決定自己看著文檔蚪腐,再重新demo一遍復(fù)習(xí)一下箭昵。

package

React Router主要包含三個包: react-router, react-router-dom,react-router-nativereact-router包含了主要的route component和功能回季。react-router-domreact-router-native提供了基于不同平臺(web和mobile)的component(類似于react需要把react和react-dom庫分開的原因家制,為了支持不同的平臺)。

Note
但是react-router-domreact-router-native中都包含了react-router這個庫泡一,因此在項目中使用的時候只需要根據(jù)你的平臺安裝上react-router-dom或者react-router-native即可

The Router

本文基于瀏覽器做平臺颤殴。首先你需要確定使用哪一種Router。
React Router中包含兩種router(被分裝成component)鼻忠。

  • <BrowserRouter>
  • <HashRouter>

區(qū)別

  • HashRouter:

    使用URL中的hash部分保持URL和頁面內(nèi)容的同步涵但。

    • 不支持location.key和location.state
  • BrowserRouter:

    URL始終和頁面的UI同步

    • 針對于服務(wù)器可以server所有URL,并返回不同的頁面

每一個Router組件只能接受一個child component帖蔓,通常會創(chuàng)建一個App Component作為子組件render app剩下的部分矮瘟。將應(yīng)用從 router 中分離對服務(wù)器端渲染有重要意義,因為我們在服務(wù)器端轉(zhuǎn)換到 <MemoryRouter> 時可以很快復(fù)用 <App> ??????

import { BrowserRouter } from 'react-router-dom'
ReactDOM.render((
  <BrowserRouter>
    <App />
  </BrowserRouter>
), document.getElementById('root'))

History

每一個Router(通常一個SPA會有一個Router)都會創(chuàng)建一個History對象塑娇,可以將這個對象理解成一個Location object數(shù)組澈侠,用來存儲瀏覽器URL的變化軌跡。雖然hostory記錄著歷史埋酬,但是你只能從中g(shù)et到當(dāng)前的Location object哨啃。

每當(dāng)?shù)刂窓诎l(fā)生改變,history對象就會改變(history存儲在react的context)写妥,因此Router component的update就開始了拳球。保證了此時頁面內(nèi)容會隨著每次URL的改變rerender。

因此Router組件必須是最外層組件耳标。

Location Object

Location object反應(yīng)的是你當(dāng)前的地址信息醇坝,包含了很多從地址欄中分出的信息

{
  pathname: '/here',
  search: '?key=value',
  hash: '#extra-information',
  state: { modal: true }, //這個是可以attach到這個location上的數(shù)據(jù)邑跪,但是不會出現(xiàn)在URL中
  key: 'abc123' //這個location的唯一標(biāo)志次坡,也可以理解成數(shù)據(jù)存放的key
}

Routes

<Route> 組件是 React Router 的主要組成部分,如果你想要在路徑符合的時候在任何地方渲染什么東西画畅,你就應(yīng)該創(chuàng)造一個 <Route> 元素砸琅。

What does the <Route> render?

每一個Route component 都必須有一個屬性,能夠描述當(dāng)這個route被match的時候應(yīng)該render些什么轴踱。以下三個屬性可以用來描述render的內(nèi)容症脂。

component

當(dāng)被match的時候,Route 組件會將會使用React.createElement創(chuàng)建一個component屬性指定類型的React component。

render

屬性的類型是一個function诱篷,這個function可以返回一個component(類似于react render props)壶唤。用于當(dāng)你想要傳遞一些屬性給被render的component的時候使用

上面兩個屬性必須使用一個
children

也是一個function return 一個component,對于上兩個屬性棕所,只有route被匹配的時候才會被render闸盔。 這個屬性,只要寫了琳省,不論任何情況這個component都會被render

Path

Route組件需要一個屬性path迎吵,描述了這個route需要匹配的pathname(僅包含port之后search之前的部分)。比如<Route path='/roster'/> 能夠匹配上的應(yīng)該是pathname以/roster開頭的所有URL针贬,一旦match上相應(yīng)的component就會被render

<Route path='/roster'/>
// when the pathname is '/', the path does not match
// when the pathname is '/roster' or '/roster/2', the path matches
// If you only want to match '/roster', then you need to use
// the "exact" prop. The following will match '/roster', but not
// '/roster/2'.
<Route exact path='/roster'/>
// You might find yourself adding the exact prop to most routes.
// In the future (i.e. v5), the exact prop will likely be true by
// default. For more information on that, you can check out this 
// GitHub issue:
// https://github.com/ReactTraining/react-router/issues/4958

Note:

  • react-router只會匹配pathname击费,也就意味著對于http://www.example.com/my-projects/one?extra=false,在react-router中等價于http://www.example.com/my-projects/one
  • react-router中route的位置桦他,決定了send的URL匹配的是哪一個route蔫巩。
  • 注意選擇是否要加參數(shù)exact, 因為不使用exact的時候快压,只要URL使用path開頭就會被匹配

render component props

對于任何一個被匹配的route批幌,這個route被render的component一定能夠接受三個屬性:

  • location:描述當(dāng)前的所在的URL

    hash: ""
    pathname: "/"
    search: ""
    state: undefined
    
  • history

    react router的Router創(chuàng)建的history對象,其中包含history API中包含的function嗓节,還有當(dāng)前URL的location對象

  • match

    isExact: true
    params: {}
    path: "/"
    url: "/"
    

match object

當(dāng)URL和某一個route match上的時候荧缘,會立刻創(chuàng)建一個match object

url 被match的URL的pathname
params path params (/a/b/:id中的id就是path params)
path route path屬性的值
isExact 是否是exact的完全匹配(path === url)

Note: match的object存儲在匹配的component的props中

使用browserRouter請求/roaster總是404

此時我的express是這樣配置的:

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.resolve(__dirname,'../','./dist')));

module.exports = app;

也就意味著,當(dāng)請求/roaster 發(fā)送過來文件/dist/roaster一定不存在拦宣,所以導(dǎo)致404截粗。

因為單頁應(yīng)用,必須要保證每一個URL發(fā)過來都應(yīng)該返回index.html然后JS文件回來瀏覽器再處理route問題

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.resolve(__dirname,'../','./dist')));

app.get('*', function (req,res) {
  res.sendFile(path.resolve(__dirname,'../','./dist/index.html'))
});

module.exports = app;

使用browserRouter請求/roaster/id JS file 404

仔細(xì)查看生成的HTML文件鸵隧,發(fā)現(xiàn)<script>標(biāo)簽包住的JS的打包文件的地址不對類似于roaster/main-12324343543.js绸罗。

原因是HTML webpack plugin生成HTML文件的時候,JS文件的請求地址是這樣src=main-12312312.js(相對的)豆瘫,這種情況會導(dǎo)致請求HTML/roaster/id珊蟀, js的請求文件路徑直接替換最后slack的部分變成roaster/main-12324343543.js

解決只需要簡單的在webpack加上publicPath

  output: {
    publicPath: '/',   
    path: __dirname + '/dist',
    filename: '[name]-[chunkhash].js'
  }

如何創(chuàng)建Nested Routes

由于Routes Component可以用在APP的任意位置,只要作為Router的child即可外驱。因此你可以使用switch component將route combine成一個group育灸。

function Main() {
  return (
    <main>
      <Switch>
        <Route path='/roster' component={Roster}/>
      </Switch>
    </main>
  );
}

function Roster() {
  return (
    <div>
      <h2>This is a roster page!</h2>
      <Switch>
        <Route exact path='/roster' component={FullRoster}/>
        <Route path='/roster/:number' component={Player}/>
      </Switch>
    </div>
  );
}
  • 先創(chuàng)建一個Main component將App的所有Route寫進(jìn)去
    • 所有用roster開頭的URL都會match到Main的Roster component
  • 然后再Roster component中再細(xì)分/roaster/之后的path

但是盡管是嵌套路由,在Roaster Component中的Routes仍然需要寫full path(是/roster/:numbe 而不是/:number

routes component傳遞data

React Router中使用route傳遞數(shù)據(jù)的方式有三種:

  • path variable:數(shù)據(jù)在URL中:

    • set:

          <Route path='/roster/:id' component={Player} />
      
          request:  /roster/111
      
    • get

      path variable一般存儲在match object中昵宇,通過match.params即可獲取一個object {id:111}磅崭。

      match object在任意一個被match的route對應(yīng)的component中都可以通過this.props.match獲取

  • query parameters:數(shù)據(jù)在query中

    • set:

       ```
           <Route path='/roster' component={Player} />
      
           request:  /roster?a=1&b=2&c=3
       ```
      
    • get

    query parameters一般存儲在location object中,通過location.search即可獲取一個string "?a=1&b=2&c=3" 注意會帶?瓦哎。

  • location.state: 數(shù)據(jù)在location object中砸喻,只有在react-router中可以使用柔逼。

    • set:由于Link的to屬性以及history.push都能能夠接受兩種類型的值:string 和 location object「畹海可以通過使用location object作為參數(shù)

        const location = {
            pathname: "/roster",
            search: "?a=1",
            state: {b:2}
        }
        
        <Link to={location} >roster</Link>
      

      一旦點擊link就會render Roster component愉适,在這個component中,通過location.state就可以獲取{b:2}

寫在最后

  1. 你可以創(chuàng)建一個沒有path屬性的Route component癣漆,這個Route會和所有URL match

關(guān)于history object

  1. history objects typically have the following properties and methods:

    • length - (number) The number of entries in the history stack
    • action - (string) The current action (PUSH, REPLACE, or POP)
    • location - (object) The current location. May have the following properties:
      • pathname - (string) The path of the URL
      • search - (string) The URL query string
      • hash - (string) The URL hash fragment
      • state - (object) location-specific state that was provided to e.g. push(path, state) when this location was pushed onto the stack. Only available in browser and memory history.
    • push(path, [state]) - (function) Pushes a new entry onto the history stack
    • replace(path, [state]) - (function) Replaces the current entry on the history stack
    • go(n) - (function) Moves the pointer in the history stack by n entries
    • goBack() - (function) Equivalent to go(-1)
    • goForward() - (function) Equivalent to go(1)
    • block(prompt) - (function) Prevents navigation (see the history docs)
  1. history對象是可變的儡毕,也就是說整個App的history對象只有一個,直接操作它會比較危險扑媚。如果你只是想獲取location的信息腰湾,可以在component中使用this.props.location獲取,而不是this.props.history.location

    class Comp extends React.Component {
      componentWillReceiveProps(nextProps) {
        // location都會創(chuàng)建新的
        const locationChanged = nextProps.location !== this.props.location
        // always true
    
        // 而history中的location永遠(yuǎn)只維護(hù)一個疆股,每次都是在這上面修改
        const locationChanged = nextProps.history.location !== this.props.history.location
      }
      // always false
    

}

```

**Note: location is also found on history.location but you shouldn’t use that because its mutable.**

https://blog.pshrmn.com/entry/a-little-bit-of-history/#anno-5

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末费坊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子旬痹,更是在濱河造成了極大的恐慌附井,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件两残,死亡現(xiàn)場離奇詭異永毅,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)人弓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門沼死,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人崔赌,你說我怎么就攤上這事意蛀。” “怎么了健芭?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵县钥,是天一觀的道長。 經(jīng)常有香客問我慈迈,道長若贮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任痒留,我火速辦了婚禮谴麦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘狭瞎。我一直安慰自己细移,他們只是感情好搏予,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布熊锭。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪碗殷。 梳的紋絲不亂的頭發(fā)上精绎,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機(jī)與錄音锌妻,去河邊找鬼代乃。 笑死,一個胖子當(dāng)著我的面吹牛仿粹,可吹牛的內(nèi)容都是我干的搁吓。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼吭历,長吁一口氣:“原來是場噩夢啊……” “哼堕仔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起晌区,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤摩骨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后朗若,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體恼五,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年哭懈,在試婚紗的時候發(fā)現(xiàn)自己被綠了灾馒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡遣总,死狀恐怖你虹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情彤避,我是刑警寧澤傅物,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站琉预,受9級特大地震影響董饰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜圆米,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一卒暂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧娄帖,春花似錦也祠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽堪旧。三九已至,卻和暖如春奖亚,著一層夾襖步出監(jiān)牢的瞬間淳梦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工昔字, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留爆袍,地道東北人。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓作郭,卻偏偏與公主長得像陨囊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子夹攒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容

  • React Router 4.0 (以下簡稱 RR4) 已經(jīng)正式發(fā)布谆扎,它遵循React的設(shè)計理念,即萬物皆組件芹助。所...
    world_7735閱讀 1,526評論 0 2
  • React-Router v4 1. 設(shè)計理念1.1. 動態(tài)路由1.2. 嵌套路由1.3. 響應(yīng)式路由 2. 快速...
    wlszouc閱讀 8,558評論 0 14
  • React Router 4.0 (以下簡稱 RR4) 已經(jīng)正式發(fā)布堂湖,它遵循React的設(shè)計理念,即萬物皆組件状土。所...
    梁相輝閱讀 97,485評論 24 195
  • react-router 本來想給大家教學(xué)react-router2.0的版本无蜂。但是考慮到4.0的版本已經(jīng)出現(xiàn)了。...
    Kris_lee閱讀 5,593評論 0 4
  • [一]聲啼哭劃長空 [剪]臍伴母一女生 [梅]蘭竹菊喜君心 [生]辰正是雪迎春 [日]月爭輝送吉言 [快]意人生揚...
    一剪梅WHX閱讀 710評論 1 3