React路由管理之React Router總結(jié)
React項(xiàng)目通常都有很多的URL需要管理,最常使用的解決方案就是React Router了,最近學(xué)習(xí)了一下,主要是看了一下官方的英文文檔,加以總結(jié),以備后查。
React Router是做什么的呢,官方的介紹是:
A complete routing library for React,keeps your UI in sync with the URL. It has a simple API with powerful features like lazy code loading, dynamic route matching, and location transition handling built right in. Make the URL your first thought, not an after-thought.
大意即:讓UI組件和URL保持同步,通過簡單的API即可實(shí)現(xiàn)強(qiáng)大的特性如:代碼懶加載,動態(tài)路由匹配,路徑過渡處理等。
下面是一些React Router的用法:
一 簡單渲染Route
有一點(diǎn)需要牢記于心,Router 是作為一個React組件,可以進(jìn)行渲染。
// ...
import { Router, Route, hashHistory } from 'react-router'
render((
<Router history={hashHistory}>
<Route path="/" component={App}/>
</Router>
), document.getElementById('app'))
這里使用了hashHistory - 它管理路由歷史與URL的哈希部分。
添加更多的路由,并指定它們對應(yīng)的組件
import About from './modules/About'
import Repos from './modules/Repos'
render((
<Router history={hashHistory}>
<Route path="/" component={App}/>
<Route path="/repos" component={Repos}/>
<Route path="/about" component={About}/>
</Router>
), document.getElementById('app'))
二 Link
// modules/App.js
import React from 'react'
import { Link } from 'react-router'
export default React.createClass({
render() {
return (
<div>
<h1>React Router Tutorial</h1>
<ul role="nav">
<li><Link to="/about">About</Link></li>
<li><Link to="/repos">Repos</Link></li>
</ul>
</div>
)
}
})
這里使用了Link 組件,它可以渲染出鏈接并使用 to 屬性指向相應(yīng)的路由。
三 嵌套路由
如果我們想添加一個導(dǎo)航欄,需要存在于每個頁面上。如果沒有路由器,我們就需要封裝一個一個nav組件,并在每一個頁面組件都引用和渲染。隨著應(yīng)用程序的增長代碼會顯得很冗余。React-router則提供了另一種方式來嵌套共享UI組件。
實(shí)際上,我們的app都是一系列嵌套的盒子,對應(yīng)的url也能夠說明這種嵌套關(guān)系:
<App> {/* url / */}
<Repos> {/* url /repos */}
<Repo/> {/* url /repos/123 */}
</Repos>
</App>
因此,可以通過把子組件嵌套到 公共組件 App上使得 App組件上的 導(dǎo)航欄 Nav 等公共部分能夠共享:
// index.js
// ...
render((
<Router history={hashHistory}>
<Route path="/" component={App}>
{/* 注意這里把兩個子組件放在Route里嵌套在了App的Route里/}
<Route path="/repos" component={Repos}/>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))
接下來,在App中將children渲染出來:
// modules/App.js
// ...
render() {
return (
<div>
<h1>React Router Tutorial</h1>
<ul role="nav">
<li><Link to="/about">About</Link></li>
<li><Link to="/repos">Repos</Link></li>
</ul>
{/* 注意這里將子組件渲染出來 */}
{this.props.children}
</div>
)
}
// ...
四 有效鏈接
Link組件和a標(biāo)簽的不同點(diǎn)之一就在于Link可以知道其指向的路徑是否是一個有效的路由。
<li><Link to="/about" activeStyle={{ color: 'red' }}>About</Link></li>
<li><Link to="/repos" activeStyle={{ color: 'red' }}>Repos</Link></li>
可以使用 activeStyle 指定有效鏈接的樣式,也可以使用activeClassName指定有效鏈接的樣式類。
大多數(shù)時候,我們并不需要知道鏈接是否有效,但在導(dǎo)航中這個特性則十分重要。比如:可以在導(dǎo)航欄中只顯示合法的路由鏈接。
// modules/NavLink.js
import React from 'react'
import { Link } from 'react-router'
export default React.createClass({
render() {
return <Link {...this.props} activeClassName="active"/>
}
})
// modules/App.js import NavLink from './NavLink' // ... <li><NavLink to="/about">About</NavLink></li> <li><NavLink to="/repos">Repos</NavLink></li>
可以在NavLink中指定只有 .active 的鏈接才顯示,這樣如果路由無效,則該鏈接就不會出現(xiàn)在導(dǎo)航欄中了。
五 URL參數(shù)
考慮下面的url:
/repos/reactjs/react-router
/repos/facebook/react
他們可能對應(yīng)的是這種形式:
/repos/:userName/:repoName
:后面是可變的參數(shù)
url中的可變參數(shù)可以通過 this.props.params[paramsName] 獲取到:
// modules/Repo.js
import React from 'react'
export default React.createClass({
render() {
return (
<div>
{/* 注意這里通過this.props.params.repoName 獲取到url中的repoName參數(shù)的值 */}
<h2>{this.props.params.repoName}</h2>
</div>
)
}
})
// index.js
// ...
// import Repo
import Repo from './modules/Repo'
render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/repos" component={Repos}/>
{/* 注意這里的路徑 帶了 :參數(shù) */}
<Route path="/repos/:userName/:repoName" component={Repo}/>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))
接下來訪問 /repos/reactjs/react-router 和 /repos/facebook/react 就會看到不同的內(nèi)容了。
六 默認(rèn)路由
// index.js
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
// and the Home component
import Home from './modules/Home'
// ...
render((
<Router history={hashHistory}>
<Route path="/" component={App}>
{/* 注意這里* /}
<IndexRoute component={Home}/>
<Route path="/repos" component={Repos}>
<Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))
這里添加了IndexRoute來指定默認(rèn)的路徑 / 所對應(yīng)的組件。注意它沒有path屬性值。
同理也有 默認(rèn)鏈接組件 IndexLink。、
七 使用Browser History
前面的例子一直使用的是hashHistory,因?yàn)樗恢笨梢赃\(yùn)行,但更好的方式是使用Browser History,它可以不依賴哈希端口 (#)。
首先需要改 index.js:
// ...
// bring in `browserHistory` instead of `hashHistory`
import { Router, Route, browserHistory, IndexRoute } from 'react-router'
render((
{/* 注意這里 */}
<Router history={browserHistory}>
{/* ... */}
</Router>
), document.getElementById('app'))
其次需要 修改webpack的本地服務(wù)配置,打開 package.json 添加 –history-api-fallback :
最后需要在 index.html中 將文件的路徑改為相對路徑:
<!-- index.html --> <!-- index.css 改為 /index.css --> <link rel="stylesheet" href="/index.css" rel="external nofollow" > <!-- bundle.js 改為 /bundle.js --> <script src="/bundle.js"></script>
這樣就去掉了url中的 # 。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React中useEffect 與 useLayoutEffect的區(qū)別
本文主要介紹了React中useEffect與useLayoutEffect的區(qū)別,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
React Native中導(dǎo)航組件react-navigation跨tab路由處理詳解
這篇文章主要給大家介紹了關(guān)于React Native中導(dǎo)航組件react-navigation跨tab路由處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
ahooks useVirtualList 封裝虛擬滾動列表
這篇文章主要為大家介紹了ahooks useVirtualList 封裝虛擬滾動列表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
詳解一個基于react+webpack的多頁面應(yīng)用配置
這篇文章主要介紹了詳解一個基于react+webpack的多頁面應(yīng)用配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
30行代碼實(shí)現(xiàn)React雙向綁定hook的示例代碼
本文主要介紹了30行代碼實(shí)現(xiàn)React雙向綁定hook的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

