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 理解的更多相关文章

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

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

  2. React Router V4发布

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

  3. [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 ...

  4. [React Router v4] Redirect to Another Page

    Overriding a browser's current location without breaking the back button or causing an infinite redi ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

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

  10. [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. ...

随机推荐

  1. 字符串连接之“+”和append

    在解决这个问题之前,需要了解的内容: 1.String是只读字符串,即String引用的字符串内容是不可改变的. 1 String s1 = "abc"; 2 s1 = " ...

  2. 洛谷 P4178 Tree

    #include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #inclu ...

  3. C#中的元组对象Tuple

    原文:C#中的元组对象Tuple 一.什么是元组 元组就是一些对象的集合,在我们编程时,比如一个人的信息,我们常常创建一个Person类去描述一个人,传统的做法如下: public class Per ...

  4. php调用系统命令的函数的比较

    了解命令的区别并进行直观的选择 这是一篇翻译文章,原作者通过表格的形式更加直观的展现出差异并进行选择 前言 PHP有众多调用系统命令的函数,大致如下: system() exec() passthru ...

  5. 案例-3D旋转木马

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. day11 python名称空间 作用域

    day11 python   一.三元运算符 def func(a, b):     return a if a > b else b   print(func(44,66))     二:函数 ...

  7. ES6(阮一峰)对象的扩展

    1.属性的简洁表示法 允许直接写入变量和函数,作为对象的属性和方法. const foo = 'bar'; const baz = {foo};//等同于 const baz = {foo : &qu ...

  8. nginx进行获取阿里云slb真实ip配置操作

    环境: 1.使用阿里云的slb进行配置nginx,nginx无法获取用户的真实ip解决方案 参考阿里云: https://help.aliyun.com/knowledge_detail/40535. ...

  9. springboot集成使用rabbitmq笔记(1.rabbitmq安装)

    使用rabbitmq笔记一 使用rabbitmq笔记二 使用rabbitmq笔记三 1.选择适配的版本,参考---https://www.rabbitmq.com/which-erlang.html ...

  10. Yii2 数据库Active Record(ORM)

    ACTIVE RECORD(ORM) 参考:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html namespace app\ ...