1.withRouter作用:把不是通过路由切换过来的组件中,将react-router 的 history、location、match 三个对象传入props对象上
 
默认情况下必须是经过路由匹配渲染的组件才存在this.props,才拥有路由参数,才能使用编程式导航的写法,执行this.props.history.push('/detail')跳转到对应路由的页面
然而不是所有组件都直接与路由相连(通过路由跳转到此组件)的,当这些组件需要路由参数时,使用withRouter就可以给此组件传入路由参数,此时就可以使用this.props
 
2.Redirect渲染<Redirect>将导航到新位置。新位置将覆盖历史堆栈中的当前位置
- to: object

要重定向到的位置。

`
<Redirect to={{
pathname: '/login',
search: '?utm=your+face',
state: { referrer: currentLocation }
}}/> - push: bool 当为true时,重定向会将新条目推入历史记录,而不是替换当前条目。 ` <Redirect push to="/somewhere/else"/> - from: string 要重定向的路径名。这只能用于在<Switch>内部呈现<Redirect>时匹配位置。
有关详细信息,请参阅<Switch children>。https://reacttraining.com/web/api/Switch/children-node ` <Switch>
<Redirect from='/old-path' to='/new-path'/>
<Route path='/new-path' component={Place}/>
</Switch>
import React, { Component } from "react";
import {
BrowserRouter as Router,
Route,
Link,
Redirect,
withRouter
} from "react-router-dom"; ////////////////////////////////////////////////////////////
// 1. Click the public page
// 2. Click the protected page
// 3. Log in
// 4. Click the back button, note the URL each time function AuthExample() {
return (
<Router>
<div>
<AuthButton />
<ul>
<li>
<Link to="/public">Public Page</Link>
</li>
<li>
<Link to="/protected">Protected Page</Link>
</li>
</ul>
<Route path="/public" component={Public} />
<Route path="/login" component={Login} />
<PrivateRoute path="/protected" component={Protected} />
</div>
</Router>
);
} /*登录控制*/
const fakeAuth = {
isAuthenticated: false,
authenticate(cb) {//登录授权
this.isAuthenticated = true;
setTimeout(cb, 100); // fake async
},
signout(cb) {//退出登录
this.isAuthenticated = false;
setTimeout(cb, 100);
}
}; /*登录成功组件*/
const AuthButton = withRouter(
/*withRouter作用:把不是通过路由切换过来的组件中,将react-router 的 history、location、match 三个对象传入props对象上*/
({ history,location,match }) =>{
// console.log(history);
// console.log(location);
// console.log(match); return fakeAuth.isAuthenticated ? (//已登录认证就显示下面的组件
<p>
Welcome!{" "}
<button
onClick={() => {//退出按钮
fakeAuth.signout(() => history.push("/"));
}}
>
Sign out
</button>
</p>
) : (//没有登录显示下面的组件
<p>You are not logged in.</p>
)
}
); /*重定向路由组件,返回的是一个Route路由*/
function PrivateRoute({ component: Component, ...rest }) {
return (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} />
) : (//没有登录就显示下面的路由匹配到登录组件
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);
} /*公共页面无需登录就能访问*/
function Public() {
return <h3>Public</h3>;
} /*登录之后访问的组件*/
function Protected() {
return <h3>Protected</h3>;
} /*登录组件*/
class Login extends Component {
state = { redirectToReferrer: false };//重定向状态 login = () => {//登录方法
fakeAuth.authenticate(() => {
this.setState({ redirectToReferrer: true });//登录进行重定向
});
}; render() {
let { from } = this.props.location.state || { from: { pathname: "/" } };
let { redirectToReferrer } = this.state;
console.log(from);//Object {pathname: "/protected", search: "", hash: "", state: undefined, key: "0hr10n"}
if (redirectToReferrer) return <Redirect to={from} />;//登录之后重定向到来时的这个组件 return (//如果没有登录就显示这个登录组件
<div>
<p>You must log in to view the page at {from.pathname}</p>
<button onClick={this.login}>Log in</button>
</div>
);
}
} export default AuthExample;

转载自https://reacttraining.com/react-router/web/example/auth-workflow

https://edu.51cto.com/lecturer/11857712.html 星星课堂web前端系列课程
https://edu.51cto.com/course/23133.html js进阶与组件化实战课程
https://edu.51cto.com/course/26063.html vue零基础入门课程
https://edu.51cto.com/course/22393.html xhtml与css基础入门课程

React-Router示例(重定向与withRouter)的更多相关文章

  1. [转] React Router 使用教程

    PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...

  2. React Router 4.x 开发,这些雷区我们都帮你踩过了

    前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...

  3. React Router教程

    React Router教程 React项目的可用的路由库是React-Router,当然这也是官方支持的.它也分为: react-router 核心组件 react-router-dom 应用于浏览 ...

  4. React Router API文档

    React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...

  5. React Router学习

    React Router教程 本教程引用马伦老师的的教程 React项目的可用的路由库是React-Router,当然这也是官方支持的.它也分为: react-router 核心组件 react-ro ...

  6. React躬行记(13)——React Router

    在网络工程中,路由能保证信息从源地址传输到正确地目的地址,避免在互联网中迷失方向.而前端应用中的路由,其功能与之类似,也是保证信息的准确性,只不过来源变成URL,目的地变成HTML页面. 在传统的前端 ...

  7. React Router 使用教程

    一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...

  8. React Router 用法

    React Router 用法 一.DEMO import React from "react"; import { HashRouter as Router, Route, Li ...

  9. react router @4 和 vue路由 详解(全)

    react router @4 和 vue路由 本文大纲: 1.vue路由基础和使用 2.react-router @4用法 3.什么是包容性路由?什么是排他性路由? 4.react路由有两个重要的属 ...

随机推荐

  1. Kettle启动时报错Cannot create java virtual machine & A java exception has occurred

    开源免费--最喜欢的四个字没有之一 1.官网下载 https://sourceforge.net/projects/pentaho/files/Data%20Integration/ 下载完后,解压即 ...

  2. xshell 连接virtualbox nat模式的虚拟主机的方式

    因为垃圾CSDN抽风无法收藏文章 所以保存了一片文章 https://blog.csdn.net/Trista_WU/article/details/79873310?utm_medium=distr ...

  3. NOIP 模拟九 考试总结

    T1 考场上先干的T2,最后慌慌张张没去想正解,打算把树建起来,拿70分的部分分,于是写树剖LCA,板子好像忘了,回忆了好久还模拟了好几遍才打对树剖LCA............期望70,结果0.考试 ...

  4. JVM学习笔记——类加载器与类加载过程

    类加载器与类加载过程 类加载器ClassLoader 类加载器 ClassLoader 用于把 class 文件装载进内存. 启动类加载器(Bootstrap ClassLoader): 这个类加载使 ...

  5. 题解 HDU 5279 YJC plays Minecraft

    题目传送门 题目大意 给出\(n\)以及\(a_{1,2,...,n}\),表示有\(n\)个完全图,第\(i\)个完全图大小为\(a_i\),这些完全图之间第\(i\)个完全图的点\(a_i\)与\ ...

  6. 自定义Push/Pop和Present/Dismiss转场

    项目概述 iOS中最常见的动画无疑是Push和Pop的转场动画了,其次是Present和Dismiss的转场动画. 如果我们想自定义这些转场动画,苹果其实提供了相关的API,在自定义转场之前,我们需要 ...

  7. java中this关键字总结

    1.this是一个引用,也是一个变量,存储在JVM堆内存的Java对象内部. 2.this变量中保存的内存地址指向自身. 3.this可以在实例方法中使用,this指向当前正在执行这个动作的对象(th ...

  8. UltraSoft - Alpha - 发布声明

    DDL_Killer Alpha版本发布声明 1. Alpha 阶段功能描述与版本实现 功能描述 设计原型 Alpha实现 登陆界面 注册界面 首页 日历视图 事项详情页 新建事项 列表视图 课程视图 ...

  9. [Beta]the Agiles Scrum Meeting 2

    会议时间:2020.5.11 20:00 1.每个人的工作 今天已完成的工作 成员 已完成的工作 yjy 修复bug将自动评测改为异步HTTP请求 tq 实现查看.删除测试点功能的后端将自动评测改为异 ...

  10. 北航OO第四单元总结

    OO最后一次博客作业--好聚好散 一.单元总结 作业一: 第一次是对类图进行解析,没有太大难度,只要根据讨论区提供的建议,新建两个类来存储相关数据即可实现. 作业二: 第二次作业的难度只有量的提升,然 ...