概述

之前写过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线程池ThreadPoolExecutor

    线程池的好处 1. 降低资源的消耗 通过重复利用已创建的线程降低线程创建和销毁所造成的消耗 2. 提高响应速度 当任务到达时,任务可以不需要等到线程创建就能立即执行 3. 提高线程的可管理型 线程是稀 ...

  2. 26.Hibernate-主键和映射.md

    目录 1.复合主键映射 [toc] 2.集合映射 2.1Set集合 2.2其他集合 [toc] 3.集合数据的读取 [toc] 4.一对多和多对一映射 4.1概念 4.2配置和开发 4.2.1关键点 ...

  3. azkaban 配置邮件

    1.配置邮件请在azkaban-web-server中进行配置:如下图:      /opt/azkaban/azkaban/azkaban-web-server/build/install/azka ...

  4. Ubuntu安装软件提示boot空间不足

    用sudo apt-get install gitlab-ci-multi-runner安装应用都会出现“gzip: stdout: No space left on device”的问题. boot ...

  5. linux环境:创建数据库用户,表空间,启动数据库

    1.启动数据库 首先使用oracle用户登录Linux,然后在shell命令行中执行下面的命令:第一步:打开Oracle监听(先查看状态:oracle监听是否启动:lsnrctl status)$ l ...

  6. 364. Nested List Weight Sum II 大小反向的括号加权求和

    [抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...

  7. Linux-目录结构及文件系统

    1.Linux 系统的顶层目录结构 /              根目录 ├── bin     存放用户二进制文件 ├── boot    存放内核引导配置文件 ├── dev     存放设备文件 ...

  8. 获取CNVD的cookie

    def getCookie(self): #获取cookie spiderFirefox = webdriver.Firefox() spiderFirefox.get("http://ww ...

  9. 32.Mysql Cluster

    32.Mysql Cluster Cluster是一组节点的组合.节点分为数据节点.SQL节点.管理节点.节点组合在一起可以为应用提供高可用.高性能.可缩放的Cluster数据管理.数据节点使用NDB ...

  10. powershell获取windows子文件夹的大小

    $startFolder = "E:\Migration\" $colItems = (Get-ChildItem $startFolder | Where-Object {$_. ...