react-router V4中的url参数
概述
之前写过react在router中传递数据的2种方法,但是有些细节没有理清楚,现在补上,记录下来,供以后开发时参考,相信对其他人也有用。
参考资料:stackoverflow react router redux url
match
如果使用下面这种方式切换路由,那么参数可以通过props.match.params.filter拿到。
<Route path='/:filter' component={App} />
<Link to='active'> Active </Link>
不过要注意2点:
- filter可以变成其它参数,这个时候props.match.params.filter里面的filter也要变成相应的参数。
- 只能在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>
注意:
- 如果要拿filter还是通过props.match.params.filter拿到。
- 只能在component={App}里面的App组件中通过这种方式拿参数。
其它
那么我们怎么在App之外的组件中获得这个参数呢?
- 一个办法是让App组件通过传递props给这个组件。
- 另一个办法是让App组件把这个参数存入redux里面。
- 还有一个办法是在这个组件前面加一个路由。(猜想的,没试过)比如这么使用:
<Route path='/:filter' component={List} />
<List />
react-router V4中的url参数的更多相关文章
- React Router V4发布
React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...
- [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 ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- [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] 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] 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 ...
随机推荐
- Garbage Disposal(模拟垃圾装垃圾口袋)
Garbage Disposal Description Enough is enough. Too many times it happened that Vasya forgot to dispo ...
- python学习笔记(五)- 文件操作
1.读文件f = open('word.txt',encoding='utf8') #默认打开当前目录下的文件,打开其它目录用绝对路径#f = open('word.txt',encoding='u ...
- PHPStorm 2018 的安装 汉化 与使用
下载地址 和安装方法 链接:https://pan.baidu.com/s/1FT8aZoQajw044qlNXkRPfg 提取码:z4sx 配置与使用方法 https://blog.csdn.net ...
- ubuntu开启远程shell,开启上传下载
需要先安装openshell-server 具体命令如下: 1.先更新下源 sudo apt-get update 2.安装openshell-server sudo apt-get install ...
- 动态创建js脚本和 css样式
//1.动态添加外部js文件 function loadScript(url){ var script = document.createElement("script"); sc ...
- linux grep (linux查找关键字在php出现的次数)
http://www.th7.cn/system/lin/201508/127681.shtml 查找CleverCode在当前目录以及子目录,所有的php出现大于0的次数. # find -type ...
- python note 05 字典及其操作
1. '''#数据类型划分:可变数据类型,不可变数据类型不可变数据类型:元组,bool int str 可哈希可变数据类型:list,dict set 不可哈希dict key 必须是不可变数据类型, ...
- python 函数进阶与闭包
函数的命名空间和作用域 引言 现在有个问题,函数里面的变量,在函数外面能直接引用么? def func1(): m = 1 print(m) print(m) #这行报的错 报错了: NameErro ...
- python 用文本来提供输入信息的模板,不用每次都手动粘贴了
#下面这一段用一个txt来保存input的信息来模拟input.最后提交代码时候删除这一段即可. a9999=open('1.txt','r') def input(): return a9999.r ...
- ABP框架系列之三十五:(MVC-Controllers-MVC控制器)
Introduction ASP.NET Boilerplate is integrated to ASP.NET MVC Controllers via Abp.Web.Mvc nuget pack ...