Overriding a browser's current location without breaking the back button or causing an infinite redirect can be tricky sometimes. In this lesson we'll learn how React Router v4 allows us to easily achieve a redirect without getting bogged down in browser history.

import React from 'react';
import { Link, Route, Redirect } from 'react-router-dom'; const Menu = () => (
<div>
<h1>Menu</h1>
<Link to="/menu/food">Food</Link>
<Link to="/menu/drinks">Drinks</Link>
<Link to="/menu/123">Redirect</Link> <Route path="/menu/:section([a-z]+)" render={({ match }) => {
return <h3>Section: {match.params.section}</h3>
}}>
</Route>
<Route path="/menu/:section(\d+)" render={({ match }) => {
return <Redirect to="/menu/food"></Redirect>
}}></Route>
</div>
); export default Menu;

So if we hit /menu/food or /menu/drink, we will go to:

        <Route path="/menu/:section([a-z]+)" render={({ match }) => {
return <h3>Section: {match.params.section}</h3>
}}>

Becase :section([a-z]+) match 'food' or 'drink'.

If we hit /menu/123, we will go to:

        <Route path="/menu/:section(\d+)" render={({ match }) => {
return <Redirect to="/menu/food"></Redirect>
}}></Route>

It will render Redirect component, and redirect us to /menu/food

[React Router v4] Redirect to Another Page的更多相关文章

  1. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

  2. [React Router v4] Intercept Route Changes

    If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm ...

  3. [React Router v4] Render Multiple Components for the Same Route

    React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...

  4. [React Router v4] Conditionally Render a Route with the Switch Component

    We often want to render a Route conditionally within our application. In React Router v4, the Route ...

  5. [React Router v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include ...

  6. [React Router v4] Use URL Parameters

    URLs can be looked at as the gateway to our data, and carry a lot of information that we want to use ...

  7. React Router V4发布

    React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...

  8. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  9. [React Router v4] Parse Query Parameters

    React Router v4 ignores query parameters entirely. That means that it is up to you to parse them so ...

随机推荐

  1. codeforces 666E. Forensic Examination(广义后缀自动机,Parent树,线段树合并)

    传送门: 解题思路: 很坑的一道题,需要离线处理,假如只有一组询问,那么就可以直接将endpos集合直接累加输出就好了. 这里就要将询问挂在树节点上,在进行线段树合并时查询就好了. 代码超级容易写挂的 ...

  2. RS-485总线和Modbus通信协议的关系

    一.RS-485总线 RS-485总线技术只是规定了接口的电气标准,并没有规定RS-485接口的电缆,插件以及通信协议,只是OSI规范中物理层的一个标准,RS-485总线采用差分平衡传输方式.由于RS ...

  3. Android学习笔记之按键操作

    我们如何和Android 程序来进行交互那份?来让 Android 程序产生相应的反应,我们不得不通过键盘事件.触摸事件.传感器事件等来实现. 键盘是Android中主要的输入设备,对按键的响应的处理 ...

  4. HTML基础-第二讲

    转自:https://blog.csdn.net/likaier/article/details/326657 我们在第一讲里概括了一下网页的主要框架,现在我们来详细的研究网页的内部细则: 先从它的身 ...

  5. 3. Vue-router 路由

    路由是根据不同的url地址展现不同的内容或页面. 前端路由就是把不同路由对应不同的内容或页面的任务交给前端来做(在单页面应用,大部分页面结构不变,只改变部分内容的使用),之前是通过服务器根据url的不 ...

  6. vb.net 调用api

    Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) ...

  7. 【CS Round #46 (Div. 1.5) E】Ultimate Orbs

    [链接]链接 [题意] n个人从左到右站在一条直线上.每个人都有一个能力值g[i],然后每个人可以将相邻的一个人打败. 然后它的能力值能够增加相应的能力值(就是打败了的那个人的能力值). A能够打败B ...

  8. Dcloud开发webApp踩过的坑

    Dcloud开发webApp踩过的坑 一.总结 一句话总结:HTML5+扩展了JavaScript对象plus,使得js可以调用各种浏览器无法实现或实现不佳的系统能力,设备能力如摄像头.陀螺仪.文件系 ...

  9. FZU《C语言程序综合设计》

    一年前的玩意. 老是有人找我要..一年前写得这么搓都不敢拿出来.... 但是好多人要啊.....直接发blog,省得下次还要发压缩文件.. 就不要吐槽我代码烂了,我也觉得很烂,至少现在看来确实很烂.. ...

  10. echarts同一页面四个图表切换的js数据交互

    需求:点击tab页,切换四个不同的图表,ajax向后台请求数据,展示在四个不同的图表中. 其余的就不多说,直接上js代码了 $(function() { $("#heart").o ...