码迷,mamicode.com
首页 > 其他好文 > 详细

[React Router v4] Use the React Router v4 Link Component for Navigation Between Routes

时间:2017-03-19 11:02:01      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:name   pat   create   previous   router   const   home   state   between   

If you’ve created several Routes within your application, you will also want to be able to navigate between them. React Router supplies a Link component that you will use to make this happen.

Import Link:

import {
    BrowserRouter as Router,
    Route, Link
} from ‘react-router-dom‘;

 

Add Nav section to the page

const Nav = () => (
  <nav>
      <Link to="/">Home</Link>
      <Link to="/about">About</Link>
      <Link replace to={{pathname: ‘/contact‘}}>Contact</Link>
  </nav>
);

There are two ways to nav to another page.

 

1. to="/about"

2. to={{pathname: ‘/contact‘}}

 

Here the ‘replace‘ keyword able to help modify pushHistory. Once you use ‘replace‘ it will replace previous state to current state, instead of add one state.

For example.

Current history: [‘/‘, ‘/about‘, ‘/contact‘], in the normal case, you hit the ‘Back‘ button in your browser it will bring you back to ‘/about‘.

But once you use ‘replace‘, it will replace [‘/‘, ‘/about‘] to [‘/‘, ‘/contact‘]. So you when hit ‘Back‘ button , you will back to ‘/‘.

[React Router v4] Use the React Router v4 Link Component for Navigation Between Routes

标签:name   pat   create   previous   router   const   home   state   between   

原文地址:http://www.cnblogs.com/Answer1215/p/6577579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!