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

本文介绍了如何在 React 应用中使用 React Router 进行页面间的导航。通过 Link 组件,可以实现不同路由间的跳转,并详细解释了常规跳转与使用 'replace' 属性进行历史状态替换的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 '/'.

转载于:https://www.cnblogs.com/Answer1215/p/6577579.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值