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 ...
随机推荐
- jquery之过滤filter,not
<body> <h1>欢迎来到我的主页</h1> <p>我是唐老鸭</p> <p class="intro"> ...
- 微信小程序--动画animation
js: list:[], contentflag:false this.animation = wx.createAnimation({ duration: 500, timingFunction ...
- vw, vh视区覆盖和自适应图片
CSS .wrap{width:100vw;height:100vh;background: rgba(0,0,0,0.3);position: fixed;top:0;left:0;text-a ...
- Linux redhat 7 进入单用户模式
redhat 7 进入单用户模式修复系统故障 1.启动机器,grub界面选择第一个,按e 2.往下翻,找到Linux16 开头的那一行 3.将ro改为"rw init=/sysroot/b ...
- 31. Next Permutation 返回下一个pumutation序列
[抄题]: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...
- React Redux 记数器
npm init react-app counter cd counter npm install 将 src/index.js改为 import React from 'react'; import ...
- node.js中使用zlib模块进行数据压缩和解压
我们可以使用 zlib 模块来对数据进行压缩和解压处理,减小数据体积,加快传输速度. 一.通过创建转换流,对文件进行压缩和解压 const fs = require('fs'); const zlib ...
- tiny4412 --uboot移植(2) 点灯
开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ...
- 彻底关闭win10后台同步数据(转自技术社区)
设置隐私里面关闭所有同步数据选项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\将下面子项属性修改 OneSyncSvc的start属相修改 ...
- js常用判断和语法
1.js获取选中的redio元素 var version = $('.version input[name="input1"]:checked').val();//单选框默认选中& ...