筆記react router 4(三)

  上一篇咱們說到了路由組件的嵌套。想必你已經運用自如了。那麼,這一次咱們來聊一聊4.X中Router的變動。javascript

在3.X中咱們若使用路由的模式,可經過在Router上配置history的值便可。java

例如,react

import { Router, Route, hashHistory} from "react-router"
import routes from "./routes"

<Router history={hashHistory} routes={routes}>
    <Route path="/" component={App}/>
</Router>

在4.X中提供幾種不一樣的路由組件來替代history屬性的做用,分別是瀏覽器

<BrowserRouter>react-router

import { BrowserRouter } from 'react-router-dom'

<BrowserRouter
  basename={optionalString}
  forceRefresh={optionalBool}
  getUserConfirmation={optionalFunc}
  keyLength={optionalNumber}
>
  <App/>
</BrowserRouter>

<HashRouter>dom

import { HashRouter } from 'react-router-dom'

<HashRouter>
  <App/>
</HashRouter>

<MemoryRouter> 通常用於測試或者無瀏覽器的環境中,像是react native中測試

<MemoryRouter initialEntries={[ '/one', '/two', { pathname: '/three' } ]} initialIndex={1} > <App/> </MemoryRouter>

<StaticRouter> 通常用於服務端,永遠不會改變locationurl

<StaticRouter location={req.url} context={context}>
      <App/>
</StaticRouter>

須要注意的是,router組件只能有一個子元素spa

相關文章
相關標籤/搜索