学习笔记——React Router Dom的基本使用

ref: https://medium.com/@pshrmn/a-simple-react-router-v4-tutorial-7f23ff27adf

react-router vs react-router-dom vs react-router-native

  • React Router has been broken into three packages: react-router, react-router-dom, and react-router-native.

react-router-dom

  • Router
    • BrowserRouter: The <BrowserRouter> should be used when you have a server that will handle dynamic requests .
    • HashRouter: while the <HashRouter> should be used for static websites (where the server can only respond to requests for files that it knows about).
    • History
    • example: Router components only expect to receive a single child element.
    import { BrowserRouter } from 'react-router-dom'
    ReactDOM.render((
    <BrowserRouter>
      <App />
    </BrowserRouter>
    ), document.getElementById('root'))
    
  • Routes
  • <Route>
    • path :which is a string that describes the pathname that the route matches.
    • exact: match the route compeletely. (will be default true after v5).
    • only match the pathname of location, don't care about the querystring.
    • use path-to-regexp package to determine if a route element's path prop matches the current location.
    • match object: when the route's path matched, a match object will be created
      • url: the matched part of the current location's pathname.
      • path: the route's path.
      • isExact: path===pathname.
      • params: an Object containing values from the pathname that were captured by path-to-regexp.
  • <Switch>
    • to group <Route>
    • will iterate over its children elements and only render the first one that matches the current pathname.
  • What does the <Route> render
    • component: A react component, no extra props to pass to the component.
    • render: A function that returns a React element
    • children: A function than returns a React element, this will always be rendered , regardless of whether the route's path matches the current location.
    <Switch>
      <Route path='/home' component={Home}/>
      <Route path='/about' render={(props) => {
        <About {...props}/>   
       }
      }/>
      <Route path='/posts' children={(props) => {
          props.match 
            ? <Posts {...props}/> 
            : <EmptyCard {...props}/>
      }}/>
    </Switch>
    
  • path params
    • for a path in <Route> , we can define a path as path='/post/:id', if we want to capture the id of the post, we can use match.params.id ,
    • note that the captured value is a string.
  • Links
    • React Router provides a <Link> component to so that we can navigate between pages, and at the same time , it's different with using anchor element, it will not reload the whole page.
    <header>
     <nav>
       <ul>
         <li><Link to='/home'>Home</Link></li>
         <li><Link to='/about'>About</Link></li>
         <li><Link to='/post'>Posts</Link></li>
     </nav>
    </header>
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,384评论 3 20
  • 如果你关注健身,那你一定知道衡量人肥胖的BMI指数。如果你热爱跑步,那你多半也知道一个用来衡量最大心率的卡式公式2...
    kafkaliu阅读 222评论 0 0
  • 在阅读今天的日课之前我很少用思维导图,反而很喜欢用列表,这是个人习惯。 思维导图作为一种思考工具而不是整理和记忆工...
    俏村姑阅读 235评论 0 1
  • 很多人读完书记不住会说这是记忆力的问题,又或者说读完书忘掉是很正常的,我们已经把知识内化成能力了。现在看起来...
    雪飞狐阅读 208评论 0 0