最近使用react router 遇到一个问题:当对某个路由添加参数的时候/list/:app 这列路由,点击这个路由之后再点击其余路由,location地址就追加到后面,问不是replace.react
/list/:app => /list/appName 切换到/info 就变成了/list/appName/info
最后经过定位,发觉是在Link的时候对Link的to没有添加绝对地址的缘由。app
<Link to="list/appName" /> 要修改成<Link to="/list/appName" />
在定位这个问题的时候,看了一下各个部分的源代码,下面对react router v4作一个简单的介绍。ide
分别从route, history, location, match 作介绍。函数
一、route 三种render方式:component, render, function
二、route的props: match, location, history
三、router的path当不指定path的时候就会一直匹配ui
route path 匹配写法:this
<Route path='tasks_list/:app/:jobId' component={component} />
若是params 是可选的写法:url
<Route path='tasks_list/:app/:jobId?' component={component} />
history 是一个路由中最重要的部分,通常来讲用户只有两种方式会更新当前 URL。一种是用户点击了某个锚标签或者直接操做 history 对象的 replace/push 方法;另外一种是用户点击前进/后退按钮。不管哪种方式都要求咱们的路由系统可以实时监听 URL 的变化,而且在 URL 发生变化时及时地作出响应,渲染出正确的页面。咱们首先来考虑下如何处理用户点击前进/后退按钮。React Router 使用 History 的 .listen 方法来监听当前 URL 的变化,其本质上仍是直接监听 HTML5 的 popstate 事件。
当监听到 popstate 事件被触发时,咱们会调用 forceUpdate 函数来强制进行重渲染。总结而言,不管咱们在系统中设置了多少的路由组件,它们都会独立地监听 popstate 事件而且相应地执行重渲染操做。接下来咱们继续讨论 matchPath 这个 Route 组件中相当重要的函数,它负责决定当前路由组件的 path 参数是否与当前 URL 相一致。code
react router 的history 是调用了ReactTraining的history
react router 的history有如下属性与方法:component
1) pathname - (string) The path of the URL
2)search - (string) The URL query string
3)hash - (string) The URL hash fragment
4)state - (string) location-specific state that was provided to e.g. push(path, state) when this location was pushed onto the stack. Only available in browser and memory history.router
1)push(path, [state]) - (function) Pushes a new entry onto the history stack 2)replace(path, [state]) - (function) Replaces the current entry on the history stack 3)go(n) - (function) Moves the pointer in the history stack by n entries 4)goBack() - (function) Equivalent to go(-1) 5)goForward() - (function) Equivalent to go(1) 6)block(prompt) - (function) Prevents navigation (see the history docs)
location指定当前的app在哪儿
Route component as this.props.location
Route render as ({ location }) => ()
Route children as ({ location }) => ()
withRouter as this.props.location
好比上面遇到的问题,就是location的问题,最后定位到是createLocation的方法出的问题。
有了location, 而后有了route, match包含的信息就是 <Route path>如何匹配当前的location
这个组件并无真实地进行界面渲染,而是仅仅进行了简单的跳转操做