以下是我结合官方文档的学习笔记,文末有官方文档的链接.
1 react-router中一个属性exact 的使用,默认是true,详见API文档解析
| 
 | 
 | 
如上,当 URL is /about /  /about两者都会render 
如下所示,当url是 /repo 的时候  Repo 和RepoTest两者也是都会渲染.当然也可以通过设置exact属性进行精确匹配.
| 
 | 
 | 
2 match对象
其中path属性值就是Route组件的path属性值,url是根据地址栏动态解析的,params是解析URL地址的时候params对象
match对象是实时生成的,当url地址不同 的时候,match对象也会跟着变化,大家可以多点击几次,看下math对象所有的属性是什么值.
当url地址栏匹配到Route对应的路径的时候,组件中的match对象才存在,否则为null
| 
 | 
 | 
A match object contains information about how a <Route path> matched the URL. match objects contain the following properties:
- params- (object) Key/value pairs parsed from the URL corresponding to the dynamic segments of the path- key值是  :id之类的变量名 value 是即时匹配到的路径字符串
 
- key值是  
- isExact- (boolean)- trueif the entire URL was matched (no trailing characters)
- path- (string) The path pattern used to match. Useful for building nested- <Route>s
- url- (string) The matched portion of the URL. Useful for building nested- <Link>s
You’ll have access match objects in various places:
- Route component as this.props.match
- Route render as ({ match }) => ()
- Route children as ({ match }) => ()
- withRouter as this.props.match
- matchPath as the return value
If a Route does not have a path, and therefore always matches, you’ll get the closest parent match. Same goes for withRouter.
Route组件要渲染的组件component,通过Router可以向该组件传递match对象;对于官网的demo我做了一些改动,方便理解
通过点击不同的链接,观察地址栏和控制台的输出,应该可以有一个基础的认识.
| 
 | 
 | 
3 Router中的组件的props对象
一般情况下,我们的组件的props属性直接通过定义可以取到,如下所示,可以通过props获取name属性的值
| 
 | 
 | 
当一个组件作为Route的component属性的值的时候,此时组件中的属性会多了location history match这三个对象
| 
 | 
 | 
| 
 | 
 | 
3 withRouter Redict
对象的解构赋值,前面仅仅是一个标记,后面才是变量
| 
 | 
 | 
源码如下
| 
 | 
 | 
学习了这么几天,我算是明白了.自己还是原来的自己,这么多年来一直没变.遇到问题总是刚正面,有时候确实很浪费时间和精力,其实有时候多看一些,多了解一些再去正面硬刚这些源码和问题,可能效率会更高.