react-router v4 理解
1、Router
(1)最基础的路由器,必须有history属性
(2)BrowserRouter和HashRouter都是由Router组件扩展而来
2、BrowserRouter
(1)BrowserRouter利用的是H5 的history API实现URL和UI同步
(2)内部的histroy对象由createBrowserHistory()生成
3、HashRouter
(1)HashRouter是通过改变URL的hash部分来实现URL和UI同步
(2)内部的histroy对象由createHashHistory()生成
4、路由跳转的方式
(1)Link和NavLink
使用NavLink的时候要注意,一般会出现第一个NavLink始终存在active类名,加一个exact属性就可以了
(2)当组件被withRouter包裹,组件的props里会又一个对象,对象里包含history、location和match属性,可以用this.props.history.push()进行路由跳转
(3)当路由器是HashRouter时,还可以自己创建history对象进行路由跳转,import { createHashHistory } from 'history' createHashHistory().push()进行路由跳转
当路由器是BrowserRouter时,createBrowserHistory().push()跳转时会导致URL改变但界面没更新,原因待研究,但是可以通过hack的方式解决:
不用BrowserRouter, 用Router代替,向Router传入封装好的自定义的history对象(createBrowserHistory()),在需要用到createBrowserHistory().push()方法的组件内再导入这个自定义history对象,就可以了
react-router v4 理解的更多相关文章
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- React Router V4发布
React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...
- [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 ...
- [React Router v4] Redirect to Another Page
Overriding a browser's current location without breaking the back button or causing an infinite redi ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [React Router v4] Use Regular Expressions with Routes
We can use regular expressions to more precisely define the paths to our routes in React Router v4. ...
随机推荐
- 【小程序】获取到的e.target与e.currentTarget区别
[小程序]获取到的e.target与e.currentTarget区别:https://blog.csdn.net/qq_33744228/article/details/80310294 使用e.t ...
- Smokeping分享
Somkeping master端搭建过程 服务端/master端 一. 搭好环境,安装依赖包(1)yum -y install httpd fping echoping curl rrdtool ...
- Linux 2.6.x fs/pipe.c local kernel root(kit?) exploit (x86)
/****************************************************************************** * .:: Impel Down ::. ...
- React-native 关于键盘遮挡界面问题
//引入 KeyboardAvoidingView import { KeyboardAvoidingView } from 'react-native'; //使用 KeyboardAvoiding ...
- Maven入门指南11:使用Nexus搭建Maven私服
1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...
- 2018-5-28-WPF-popup置顶
title author date CreateTime categories WPF popup置顶 lindexi 2018-05-28 09:58:53 +0800 2018-2-13 17:2 ...
- 前端,用js根据一个对象,去除另个对象中重复的元素
这里的应用场景是,两个div盛放待选项目和已选项目,如下图 <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6"> < ...
- PHP-组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- Tomcat启动后中文乱码,怎么解决这个问题
今天很疑惑这个问题,于是去网上找了答案,结果是需要修改Tomcat根目录下面的"logging.properties"文件,把所有的encoding=UTF-8的改成encodng ...
- 9、Python 连接 PostgreSQL数据库 -- psycopg2
1.cmd pip install psycopg2 -- 提示错误信息 2.pip show pip -->查看当前pip版本 3.python -m pip install --upg ...