成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

ReactRouter-V4 構建之道與源碼分析

開發 開發工具
本文即是我在構建 React Router V4 過程中的考慮以及所謂路由即組件思想的落地實踐。

[[188988]]

多年之后當我回想起初學客戶端路由的那個下午,滿腦子里充斥著的只是對于單頁應用的驚嘆與漿糊。彼時我還是將應用代碼與路由代碼當做兩個獨立的部分進行處理,就好像同父異母的兄弟盡管不喜歡對方但是不得不在一起。幸而這些年里我能夠和其他優秀的開發者進行交流,了解他們對于客戶端路由的看法。盡管他們中的大部分與我“英雄所見略同”,但是我還是找到了合適的平衡路由的抽象程度與復雜程度的方法。本文即是我在構建 React Router V4 過程中的考慮以及所謂路由即組件思想的落地實踐。首先我們來看下我們在構建路由過程中的測試代碼,你可以用它來測試你的自定義路由:

  1. const Home = () => ( 
  2.   <h2>Home</h2> 
  3.  
  4. const About = () => ( 
  5.   <h2>About</h2> 
  6.  
  7. const Topic = ({ topicId }) => ( 
  8.   <h3>{topicId}</h3> 
  9.  
  10. const Topics = ({ match }) => { 
  11.   const items = [ 
  12.     { name'Rendering with React', slug: 'rendering' }, 
  13.     { name'Components', slug: 'components' }, 
  14.     { name'Props v. State', slug: 'props-v-state' }, 
  15.   ] 
  16.  
  17.   return ( 
  18.     <div> 
  19.       <h2>Topics</h2> 
  20.       <ul> 
  21.         {items.map(({ name, slug }) => ( 
  22.           <li key={name}> 
  23.             <Link to={`${match.url}/${slug}`}>{name}</Link> 
  24.           </li> 
  25.         ))} 
  26.       </ul> 
  27.       {items.map(({ name, slug }) => ( 
  28.         <Route key={name} path={`${match.path}/${slug}`} render={() => ( 
  29.           <Topic topicId={name} /> 
  30.         )} /> 
  31.       ))} 
  32.       <Route exact path={match.url} render={() => ( 
  33.         <h3>Please select a topic.</h3> 
  34.       )}/> 
  35.     </div> 
  36.   ) 
  37.  
  38. const App = () => ( 
  39.   <div> 
  40.     <ul> 
  41.       <li><Link to="/">Home</Link></li> 
  42.       <li><Link to="/about">About</Link></li> 
  43.       <li><Link to="/topics">Topics</Link></li> 
  44.     </ul> 
  45.  
  46.     <hr/> 
  47.  
  48.     <Route exact path="/" component={Home}/> 
  49.     <Route path="/about" component={About}/> 
  50.     <Route path="/topics" component={Topics} /> 
  51.   </div> 

如果你對于 React Router v4 尚不是完全了解,我們先對上述代碼中涉及到的相關關鍵字進行解釋。Route 會在當前 URL 與 path 屬性值相符的時候渲染相關組件,而 Link 提供了聲明式的,易使用的方式來在應用內進行跳轉。換言之,Link 組件允許你更新當前 URL,而 Route 組件則是根據 URL 渲染組件。本文并不專注于講解 RRV4 的基礎概念,你可以前往官方文檔了解更多知識;本文是希望介紹我在構建 React Router V4 過程中的思維考慮過程,值得一提的是,我很欣賞 React Router V4 中的 Just Components 概念,這一點就不同于 React Router 之前的版本中將路由與組件隔離來看,而允許了路由組件像普通組件一樣自由組合。相信對于 React 組件相當熟悉的開發者絕不會陌生于如何將路由組件嵌入到正常的應用中。

Route

我們首先來考量下如何構建Route組件,包括其暴露的 API,即 Props。在我們上面的示例中,我們會發現Route組件包含三個 Props:exact、path 以及 component。這也就意味著我們的propTypes聲明如下:

  1. static propTypes = { 
  2.   exact: PropTypes.bool, 
  3.   path: PropTypes.string, 
  4.   component: PropTypes.func, 

這里有一些微妙的細節需要考慮,首先對于path并沒有設置為必須參數,這是因為我們認為對于沒有指定關聯路徑的Route組件應該自動默認渲染。而component參數也沒有被設置為必須是因為我們提供了其他的方式進行渲染,譬如render函數:

  1. <Route path='/settings' render={({ match }) => { 
  2.   return <Settings authed={isAuthed} match={match} /> 
  3. }} /> 

render 函數允許你方便地使用內聯函數來創建 UI 而不是創建新的組件,因此我們也需要將該函數設置為 propTypes:

  1. static propTypes = { 
  2.   exact: PropTypes.bool, 
  3.   path: PropTypes.string, 
  4.   component: PropTypes.func, 
  5.   render: PropTypes.func, 

在確定了 Route 需要接收的組件參數之后,我們需要來考量其實際功能;Route 核心的功能在于能夠當 URL 與 path 屬性相一致時執行渲染操作。基于這個論斷,我們首先需要實現判斷是否匹配的功能,如果判斷為匹配則執行渲染否則返回空值。我們在這里將該函數命名為 matchPatch,那么此時整個 Route 組件的 render 函數定義如下:

  1. class Route extends Component { 
  2.   static propTypes = { 
  3.     exact: PropTypes.bool, 
  4.     path: PropTypes.string, 
  5.     component: PropTypes.func, 
  6.     render: PropTypes.func, 
  7.   } 
  8.  
  9.   render () { 
  10.     const { 
  11.       path, 
  12.       exact, 
  13.       component, 
  14.       render, 
  15.     } = this.props 
  16.  
  17.     const match = matchPath( 
  18.       location.pathname, // global DOM variable 
  19.       { path, exact } 
  20.     ) 
  21.  
  22.     if (!match) { 
  23.       // Do nothing because the current 
  24.       // location doesn't match the path prop. 
  25.  
  26.       return null 
  27.     } 
  28.  
  29.     if (component) { 
  30.       // The component prop takes precedent over the 
  31.       // render method. If the current location matches 
  32.       // the path prop, create a new element passing in 
  33.       // match as the prop. 
  34.  
  35.       return React.createElement(component, { match }) 
  36.     } 
  37.  
  38.     if (render) { 
  39.       // If there's a match but component 
  40.       // was undefined, invoke the render 
  41.       // prop passing in match as an argument. 
  42.  
  43.       return render({ match }) 
  44.     } 
  45.  
  46.     return null 
  47.   } 

現在的 Route 看起來已經相對明確了,當路徑相匹配的時候才會執行界面渲染,否則返回為空。現在我們再回過頭來考慮客戶端路由中常見的跳轉策略,一般來說用戶只有兩種方式會更新當前 URL。一種是用戶點擊了某個錨標簽或者直接操作 history 對象的 replace/push 方法;另一種是用戶點擊前進/后退按鈕。無論哪一種方式都要求我們的路由系統能夠實時監聽 URL 的變化,并且在 URL 發生變化時及時地做出響應,渲染出正確的頁面。我們首先來考慮下如何處理用戶點擊前進/后退按鈕。React Router 使用 History 的 .listen 方法來監聽當前 URL 的變化,其本質上還是直接監聽 HTML5 的 popstate 事件。popstate 事件會在用戶點擊某個前進/后退按鈕的時候觸發;而在每次重渲染的時候,每個 Route 組件都會重現檢測當前 URL 是否匹配其預設的路徑參數。

  1. class Route extends Component { 
  2.   static propTypes: { 
  3.     path: PropTypes.string, 
  4.     exact: PropTypes.bool, 
  5.     component: PropTypes.func, 
  6.     render: PropTypes.func, 
  7.   } 
  8.  
  9.   componentWillMount() { 
  10.     addEventListener("popstate", this.handlePop) 
  11.   } 
  12.  
  13.   componentWillUnmount() { 
  14.     removeEventListener("popstate", this.handlePop) 
  15.   } 
  16.  
  17.   handlePop = () => { 
  18.     this.forceUpdate() 
  19.   } 
  20.  
  21.   render() { 
  22.     const { 
  23.       path, 
  24.       exact, 
  25.       component, 
  26.       render, 
  27.     } = this.props 
  28.  
  29.     const match = matchPath(location.pathname, { path, exact }) 
  30.  
  31.     if (!match) 
  32.       return null 
  33.  
  34.     if (component) 
  35.       return React.createElement(component, { match }) 
  36.  
  37.     if (render) 
  38.       return render({ match }) 
  39.  
  40.     return null 
  41.   } 

你會發現上面的代碼與之前的相比多了掛載與卸載 popstate 監聽器的功能,其會在組件掛載時添加一個 popstate 監聽器;當監聽到 popstate 事件被觸發時,我們會調用 forceUpdate 函數來強制進行重渲染。總結而言,無論我們在系統中設置了多少的路由組件,它們都會獨立地監聽 popstate 事件并且相應地執行重渲染操作。接下來我們繼續討論 matchPath 這個 Route 組件中至關重要的函數,它負責決定當前路由組件的 path 參數是否與當前 URL 相一致。這里還必須提下我們設置的另一個 Route 的參數 exact,其用于指明路徑匹配策略;當 exact 值被設置為 true 時,僅當路徑完全匹配于 location.pathname 才會被認為匹配成功:

pathlocation.pathnameexactmatches?/one/one/twotrueno/one/one/twofalseyes

讓我們深度了解下 matchPath 函數的工作原理,該函數的簽名如下:

  1. const match = matchPath(location.pathname, { path, exact }) 

其中函數的返回值 match 應該根據路徑是否匹配的情況返回為空或者一個對象。基于這些推導我們可以得出 matchPatch 的原型:

  1. const matchPath = (pathname, options) => { 
  2.   const { exact = false, path } = options 

這里我們使用 ES6 的解構賦值,當某個屬性未定義時我們使用預定義地默認值,即 false。我在上文提及的 path 非必要參數的具體支撐實現就在這里,我們首先進行空檢測,當發現 path 為未定義或者為空時則直接返回匹配成功:

  1. const matchPath = (pathname, options) => { 
  2.   const { exact = false, path } = options 
  3.  
  4.   if (!path) { 
  5.     return { 
  6.       path: null
  7.       url: pathname, 
  8.       isExact: true
  9.     } 
  10.   } 

接下來繼續考慮具體執行匹配的部分,React Router 使用了 pathToRegex 來檢測是否匹配,即可以用簡單的正則表達式:

  1. const matchPath = (pathname, options) => { 
  2.   const { exact = false, path } = options 
  3.  
  4.   if (!path) { 
  5.     return { 
  6.       path: null
  7.       url: pathname, 
  8.       isExact: true
  9.     } 
  10.   } 
  11.  
  12.   const match = new RegExp(`^${path}`).exec(pathname) 
  13.  

這里使用的 .exec 函數,會在包含指定的文本時返回一個數組,否則返回空值;下表即是當我們的路由設置為 /topics/components時具體的返回:

  1. | path                    | location.pathname    | return value             | 
  2. ----------------------- | -------------------- | ------------------------ | 
  3. | `/`                     | `/topics/components` | `['/']`                  | 
  4. | `/about`                | `/topics/components` | `null`                   | 
  5. | `/topics`               | `/topics/components` | `['/topics']`            | 
  6. | `/topics/rendering`     | `/topics/components` | `null`                   | 
  7. | `/topics/components`    | `/topics/components` | `['/topics/components']` | 
  8. | `/topics/props-v-state` | `/topics/components` | `null`                   | 
  9. | `/topics`               | `/topics/components` | `['/topics']`            | 

這里大家就會看出來,我們會為每個 <Route> 實例創建一個 match 對象。在獲取到 match 對象之后,我們需要再做如下判斷是否匹配:

  1. const matchPath = (pathname, options) => { 
  2.   const { exact = false, path } = options 
  3.  
  4.   if (!path) { 
  5.     return { 
  6.       path: null
  7.       url: pathname, 
  8.       isExact: true
  9.     } 
  10.   } 
  11.  
  12.   const match = new RegExp(`^${path}`).exec(pathname) 
  13.  
  14.   if (!match) { 
  15.     // There wasn't a match. 
  16.     return null 
  17.   } 
  18.  
  19.   const url = match[0] 
  20.   const isExact = pathname === url 
  21.  
  22.   if (exact && !isExact) { 
  23.     // There was a match, but it wasn't 
  24.     // an exact match as specified by 
  25.     // the exact prop. 
  26.  
  27.     return null 
  28.   } 
  29.  
  30.   return { 
  31.     path, 
  32.     url, 
  33.     isExact, 
  34.   } 

Link

上文我們已經提及通過監聽 popstate 狀態來響應用戶點擊前進/后退事件,現在我們來考慮通過構建 Link 組件來處理用戶通過點擊錨標簽進行跳轉的事件。Link 組件的 API 應該如下所示:

  1. <Link to='/some-path' replace={false} /> 

其中的 to 是一個指向跳轉目標地址的字符串,而 replace 則是布爾變量來指定當用戶點擊跳轉時是替換 history 棧中的記錄還是插入新的記錄。基于上述的 API 設計,我們可以得到如下的組件聲明:

  1. class Link extends Component { 
  2.   static propTypes = { 
  3.     to: PropTypes.string.isRequired, 
  4.     replace: PropTypes.bool, 
  5.   } 

現在我們已經知道 Link 組件的渲染函數中需要返回一個錨標簽,不過我們的前提是要避免每次用戶切換路由的時候都進行整頁的刷新,因此我們需要為每個錨標簽添加一個點擊事件的處理器:

  1. class Link extends Component { 
  2.   static propTypes = { 
  3.     to: PropTypes.string.isRequired, 
  4.     replace: PropTypes.bool, 
  5.   } 
  6.  
  7.   handleClick = (event) => { 
  8.     const { replaceto } = this.props 
  9.     event.preventDefault() 
  10.  
  11.     // route here. 
  12.   } 
  13.  
  14.   render() { 
  15.     const { to, children} = this.props 
  16.  
  17.     return ( 
  18.       <a href={to} onClick={this.handleClick}> 
  19.         {children} 
  20.       </a> 
  21.     ) 
  22.   } 

這里實際的跳轉操作我們還是執行 History 中的抽象的 push 與 replace 函數,在使用 browserHistory 的情況下我們本質上還是使用 HTML5 中的 pushState 與 replaceState 函數。pushState 與 replaceState 函數都要求輸入三個參數,首先是一個與***的歷史記錄相關的對象,在 React Router 中我們并不需要該對象,因此直接傳入一個空對象;第二個參數是標題參數,我們同樣不需要改變該值,因此直接傳入空即可;***第三個參數則是我們需要的,用于指明新的相對地址的字符串:

  1. const historyPush = (path) => { 
  2.   history.pushState({}, null, path) 
  3.  
  4. const historyReplace = (path) => { 
  5.   history.replaceState({}, null, path) 

而后在 Link 組件內,我們會根據 replace 參數來調用 historyPush 或者 historyReplace 函數:

  1. class Link extends Component { 
  2.   static propTypes = { 
  3.     to: PropTypes.string.isRequired, 
  4.     replace: PropTypes.bool, 
  5.   } 
  6.   handleClick = (event) => { 
  7.     const { replaceto } = this.props 
  8.     event.preventDefault() 
  9.  
  10.     replace ? historyReplace(to) : historyPush(to
  11.   } 
  12.  
  13.   render() { 
  14.     const { to, children} = this.props 
  15.  
  16.     return ( 
  17.       <a href={to} onClick={this.handleClick}> 
  18.         {children} 
  19.       </a> 
  20.     ) 
  21.   } 

組件注冊

現在我們需要考慮如何保證用戶點擊了 Link 組件之后觸發全部路由組件的檢測與重渲染。在我們上面實現的 Link 組件中,用戶執行跳轉之后瀏覽器的顯示地址會發生變化,但是頁面尚不能重新渲染;我們聲明的 Route 組件并不能收到相應的通知。為了解決這個問題,我們需要追蹤那些顯現在界面上實際被渲染的 Route 組件并且當路由變化時調用它們的 forceUpdate 方法。React Router 主要通過有機組合 setState、context 以及 history.listen 方法來實現該功能。每個 Route 組件被掛載時我們會將其加入到某個數組中,然后當位置變化時,我們可以遍歷該數組然后對每個實例調用 forceUpdate 方法:

  1. let instances = [] 
  2.  
  3. const register = (comp) => instances.push(comp) 
  4. const unregister = (comp) => instances.splice(instances.indexOf(comp), 1) 

這里我們創建了兩個函數,當 Route 掛載時調用 register 函數,而卸載時調用 unregister 函數。然后無論何時調用 historyPush 或者 historyReplace 函數時都會遍歷實例數組中的對象的渲染方法,此時我們的 Route 組件就需要聲明為如下樣式:

  1. class Route extends Component { 
  2.   static propTypes: { 
  3.     path: PropTypes.string, 
  4.     exact: PropTypes.bool, 
  5.     component: PropTypes.func, 
  6.     render: PropTypes.func, 
  7.   } 
  8.  
  9.   componentWillMount() { 
  10.     addEventListener("popstate", this.handlePop) 
  11.     register(this) 
  12.   } 
  13.  
  14.   componentWillUnmount() { 
  15.     unregister(this) 
  16.     removeEventListener("popstate", this.handlePop) 
  17.   } 
  18.  
  19.   ... 

然后我們需要更新 historyPush 與 historyReplace 函數:

  1. const historyPush = (path) => { 
  2.   history.pushState({}, null, path) 
  3.   instances.forEach(instance => instance.forceUpdate()) 
  4.  
  5. const historyReplace = (path) => { 
  6.   history.replaceState({}, null, path) 
  7.   instances.forEach(instance => instance.forceUpdate()) 

這樣的話就保證了無論何時用戶點擊 <Link> 組件之后,在位置顯示變化的同時,所有的 <Route> 組件都能夠被通知到并且執行重匹配與重渲染。現在我們完整的路由解決方案就成形了:

  1. import React, { PropTypes, Component } from 'react' 
  2.  
  3. let instances = [] 
  4.  
  5. const register = (comp) => instances.push(comp) 
  6. const unregister = (comp) => instances.splice(instances.indexOf(comp), 1) 
  7.  
  8. const historyPush = (path) => { 
  9.   history.pushState({}, null, path) 
  10.   instances.forEach(instance => instance.forceUpdate()) 
  11.  
  12. const historyReplace = (path) => { 
  13.   history.replaceState({}, null, path) 
  14.   instances.forEach(instance => instance.forceUpdate()) 
  15.  
  16. const matchPath = (pathname, options) => { 
  17.   const { exact = false, path } = options 
  18.  
  19.   if (!path) { 
  20.     return { 
  21.       path: null
  22.       url: pathname, 
  23.       isExact: true 
  24.     } 
  25.   } 
  26.  
  27.   const match = new RegExp(`^${path}`).exec(pathname) 
  28.  
  29.   if (!match) 
  30.     return null 
  31.  
  32.   const url = match[0] 
  33.   const isExact = pathname === url 
  34.  
  35.   if (exact && !isExact) 
  36.     return null 
  37.  
  38.   return { 
  39.     path, 
  40.     url, 
  41.     isExact, 
  42.   } 
  43.  
  44. class Route extends Component { 
  45.   static propTypes: { 
  46.     path: PropTypes.string, 
  47.     exact: PropTypes.bool, 
  48.     component: PropTypes.func, 
  49.     render: PropTypes.func, 
  50.   } 
  51.  
  52.   componentWillMount() { 
  53.     addEventListener("popstate", this.handlePop) 
  54.     register(this) 
  55.   } 
  56.  
  57.   componentWillUnmount() { 
  58.     unregister(this) 
  59.     removeEventListener("popstate", this.handlePop) 
  60.   } 
  61.  
  62.   handlePop = () => { 
  63.     this.forceUpdate() 
  64.   } 
  65.  
  66.   render() { 
  67.     const { 
  68.       path, 
  69.       exact, 
  70.       component, 
  71.       render, 
  72.     } = this.props 
  73.  
  74.     const match = matchPath(location.pathname, { path, exact }) 
  75.  
  76.     if (!match) 
  77.       return null 
  78.  
  79.     if (component) 
  80.       return React.createElement(component, { match }) 
  81.  
  82.     if (render) 
  83.       return render({ match }) 
  84.  
  85.     return null 
  86.   } 
  87.  
  88. class Link extends Component { 
  89.   static propTypes = { 
  90.     to: PropTypes.string.isRequired, 
  91.     replace: PropTypes.bool, 
  92.   } 
  93.   handleClick = (event) => { 
  94.     const { replaceto } = this.props 
  95.  
  96.     event.preventDefault() 
  97.     replace ? historyReplace(to) : historyPush(to
  98.   } 
  99.  
  100.   render() { 
  101.     const { to, children} = this.props 
  102.  
  103.     return ( 
  104.       <a href={to} onClick={this.handleClick}> 
  105.         {children} 
  106.       </a> 
  107.     ) 
  108.   } 

另外,React Router API 中提供了所謂 <Redirect> 組件,允許執行路由跳轉操作:

  1. class Redirect extends Component { 
  2.   static defaultProps = { 
  3.     push: false 
  4.   } 
  5.  
  6.   static propTypes = { 
  7.     to: PropTypes.string.isRequired, 
  8.     push: PropTypes.bool.isRequired, 
  9.   } 
  10.  
  11.   componentDidMount() { 
  12.     const { to, push } = this.props 
  13.  
  14.     push ? historyPush(to) : historyReplace(to
  15.   } 
  16.  
  17.   render() { 
  18.     return null 
  19.   } 

注意這個組件并沒有真實地進行界面渲染,而是僅僅進行了簡單的跳轉操作。到這里本文也就告一段落了,希望能夠幫助你去了解 React Router V4 的設計思想以及 Just Component 的接口理念。我一直說 React 會讓你成為更加優秀地開發者,而 React Router 則會是你不小的助力。

【本文是51CTO專欄作者“張梓雄 ”的原創文章,如需轉載請通過51CTO與作者聯系】

戳這里,看該作者更多好文

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2021-02-16 10:55:02

Nodejs模塊

2016-10-21 13:03:18

androidhandlerlooper

2021-09-08 10:47:33

Flink執行流程

2016-11-25 13:26:50

Flume架構源碼

2016-11-29 09:38:06

Flume架構核心組件

2016-11-25 13:14:50

Flume架構源碼

2020-06-04 12:15:08

Pythonkafka代碼

2021-08-06 15:06:09

騰訊開源Apache

2009-12-22 13:36:39

Linux Sysfs

2009-11-30 17:33:07

微軟

2013-06-26 10:25:39

2022-04-05 12:59:07

源碼線程onEvent

2016-11-29 16:59:46

Flume架構源碼

2014-08-26 11:11:57

AsyncHttpCl源碼分析

2011-03-15 11:33:18

iptables

2021-10-27 11:29:49

Linux框架內核

2009-09-28 10:49:13

ITIL摩卡

2017-10-25 13:20:43

軟件安全模型

2011-06-08 09:22:54

Samba

2009-12-25 15:21:00

WPF構建前臺
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久在线观看| 99久久精品国产一区二区三区 | 国产成人免费视频网站视频社区 | 99亚洲精品视频 | 国产成人精品一区二区三区在线 | 人人插人人 | 日一区二区 | 亚洲小视频 | 成人国产午夜在线观看 | 国产免费又色又爽又黄在线观看 | av在线一区二区三区 | 欧美视频在线观看 | 天天综合日日夜夜 | 欧美二级| 精品国产一区二区三区久久 | 日本大香伊一区二区三区 | 理论片免费在线观看 | 日韩视频观看 | 国产福利91精品 | 成人小视频在线观看 | 亚洲精品91 | 欧美午夜一区 | 久久综合九色综合欧美狠狠 | 久久精品一区二区三区四区 | 一区在线观看 | 久久亚洲一区二区三区四区 | 一级欧美 | 精品一区二区三区在线播放 | 拍真实国产伦偷精品 | 伊人精品在线视频 | 亚洲性免费 | 亚洲iv一区二区三区 | 欧美视频精品 | 黄色大全免费看 | a精品视频| 综合色久 | 久草网在线视频 | 精品久久电影 | 国产精品久久久久永久免费观看 | 一级毛片免费 | 久久av影院|