概述

之前写过react在router中传递数据的2种方法,但是有些细节没有理清楚,现在补上,记录下来,供以后开发时参考,相信对其他人也有用。

参考资料:stackoverflow react router redux url

match

如果使用下面这种方式切换路由,那么参数可以通过props.match.params.filter拿到。

<Route path='/:filter' component={App} />

<Link to='active'> Active </Link>

不过要注意2点:

  1. filter可以变成其它参数,这个时候props.match.params.filter里面的filter也要变成相应的参数。
  2. 只能在component={App}里面的App组件中通过props拿到filter这个参数,在其它组件中拿不到。(即不是组件自身渲染时通过解析url拿到参数的,而是通过props传递过来的。)

location

如果使用下面这种方式切换路由,那么参数data可以通过props.location.data.name拿到。

const linkActive = {
pathname: 'active',
data: {name: 'haha'}
} <Route path='/:filter' component={App} /> <Link to={ linkActive }> Active </Link>

注意:

  1. 如果要拿filter还是通过props.match.params.filter拿到。
  2. 只能在component={App}里面的App组件中通过这种方式拿参数。

其它

那么我们怎么在App之外的组件中获得这个参数呢?

  1. 一个办法是让App组件通过传递props给这个组件。
  2. 另一个办法是让App组件把这个参数存入redux里面。
  3. 还有一个办法是在这个组件前面加一个路由。(猜想的,没试过)比如这么使用:
<Route path='/:filter' component={List} />
<List />

react-router V4中的url参数的更多相关文章

  1. React Router V4发布

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

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

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

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

  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] Intercept Route Changes

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

  6. [React Router v4] Redirect to Another Page

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

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

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

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

随机推荐

  1. JAVA四则运算(读写文件)

    完成时间:17:10 package 四则运算试题; import java.io.BufferedReader; import java.io.PrintStream; import java.ut ...

  2. python入门(四):字符串、编码、random

    1.字符串 字符串基本有两种类型,str和bytes >>> s="a" >>> type(s) <class 'str'>     ...

  3. 大数据入门到精通13--为后续和MySQL数据库准备

    We will be using the sakila database extensively inside the rest of the course and it would be great ...

  4. “AS3.0高级动画编程”学习:第三章等角投影(上)

    什么是等角投影(isometric)? 原作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com 刚接触这个概念时,我也很茫然,百度+google了N天后,找到了一些文章: [转 ...

  5. 十八、Memento 备忘录设计模式

    原理: 代码清单: Memento public class Memento { int mondey; ArrayList fruits; Memento(int mondey){ this.mon ...

  6. Windbg断点调试

    [文章主题] Windbg是Windows驱动调试的重要软件,也是必须学习的软件,前面的博客介绍了一些双机调试的环境配置,只要按照我所说的步骤一步步下来就可以完成环境搭建. 本文主要介绍如何调试sys ...

  7. dede织梦动态页面通过手机模板实现wap浏览

    https://jingyan.baidu.com/article/a948d6517be0eb0a2dcd2ebc.html

  8. 20175314 《Java程序设计》第四周学习总结

    20175314 <Java程序设计>第四周学习总结 教材学习内容总结 每个子类只能有一个父类,而一个父类可以有多个子类.可以使用关键字extends来定义一个类的子类:class 子类名 ...

  9. C++成员函数在内存中的存储方式

    用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码分 ...

  10. ACM-ICPC 2018 南京赛区网络预赛 J.sum(欧拉筛)

    题目来源:https://nanti.jisuanke.com/t/A1956 题意:找一个数拆成无平方因子的组合数,然后求前缀和. 解题思路:我们可以把某个数分解质因数,如果某个数可以分解出三个相同 ...