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 ...
随机推荐
- 【原】The Linux Command Line - Workiing with commands
● type – Indicate how a command name is interpreted● which – Display which executable program will b ...
- transform(转)
转自:https://zhuanlan.zhihu.com/p/54356280
- c#mvc实现登录
本篇介绍MVC实现登录的方式,如下: 1.通过MVC Form 表单请求实现登录 2.通过AJAX GET 请求MVC Controller 实现登录 3.通过AJAX POST 请求MVC Cont ...
- 十七、 Observer 观察者设计模式
设计: 代码清单: Observer public interface Observer { void update(NumberGenerator generator); } DigitObserv ...
- NodeJs第3方包说明
formidable 作用:实现简单文件上传 var formidable = require('formidable'); var form = new formidable.IncomingFor ...
- JavaSE基础知识(3)—流程控制结构
一.顺序结构 1.说明 程序从上往下依次执行,中间没有任何跳转或选择2.特点 变量必须遵循 “前向引用” (局部变量必须先声明.赋值,然后再使用!) 二.分支结构(条件) 1.说明 程序从两条或多条路 ...
- 如何在chrome上打开SSL3.0
Chrome默认关闭对SSL3.0的支持,无法访问一些Web应用.可以手动打开他. 启动chrome依次选择 设置->高级->系统->打开代理设置->安全 将使用SSL 3.0 ...
- iOS开发第三方库一 IQKeyboardManager
每一个iOS应用的开发者在工作中都会遇到需要用户键盘输入数据的需求,而输入框(UITextField/UITextView)的父界面可能是普通的UIView,也可能是UIScrollView,UITa ...
- 20165213Java第二次实验
实验二Java面向对象程序设计 实验1 实验目的与要求: 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交 ...
- mysql error#1251客户端版本过低
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; Query OK, 0 ...