概述

之前写过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 集合是否有序

    参考:https://www.cnblogs.com/hoobey/p/5914226.html

  2. Java中锁的实现与内存语义

    目录 1. 概述 2. 锁的内存语义 3. 锁内存语义的实现 4. 总结 1. 概述 锁在实际使用时只是明白锁限制了并发访问, 但是锁是如何实现并发访问的, 同学们可能不太清楚, 下面这篇文章就来揭开 ...

  3. linux创建快捷方式ln命令

    创建快捷方式命令 ln -s 源文件 目标目录 //目标目录可以是完整路径,也可以是当前目录下的路径 ln 源文件 目标目录 在桌面上添加一个,创建一个文件夹(这里是work)的快捷方式 //源 cd ...

  4. 浅析AnyCast网络技术

    什么是BGP AnyCast? BGP anycast就是利用一个(多个) as号码在不同的地区广播相同的一个ip段.利用bgp的寻路原则,短的as path 会选成最优路径(bgp寻路原则之n),从 ...

  5. 关于EL表达式随笔记录

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. hdu 4714 树+DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...

  7. LAB2

    任务1: 效果:HelloWorld 好像完全按视频做就行了 学会了:把glassfish改好了,能跑 没学会:视频里的解说不懂在干嘛,得再看看 任务2 效果:intersetingpicture要求 ...

  8. 数字提取——C语言

    Problem Description AekdyCoin is the most powerful boy in the group ACM_DIY, whose signature is valu ...

  9. 新版本wireshark tshark使用

    Wireshark-tshark wireshark 指令模式 => tshark Windows 及Linux 可至安裝目錄執行>tshark tshark.exe -i 7(利用-D找 ...

  10. spring rabbitmq集成

    pom.xml加 <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-clien ...