I. Three types of Components:
1. router components
create a history object, 提供“前进”、“后退”
<BrowserRouter>
: for server that responds to requests<HashRouter>
: for a static file server.
2. route matching components
comparing a
‘s path prop with the current location’s pathname
只要路由匹配,Route所在位置就会render相应的Component
<Route>
:包容性路由- 3个props:
- path:匹配路径
- component:对应的组件
- render:没有可用组件时,直接render
- 1个state:
- match
- match包含4个field:
isExact
,params
,path
,url
- 1个context:
- router obj
- router 包含
history
,routes
- 3个props:
<Switch>
:排他性路由1
2
3
4
5
6
7
8<Switch>
<Route exact path='/' component= {Home}/>
<Route path='/about' component={About}/>
<Route path='/contact' component={Contact}/>
<Route component={NoMatch}/>
</Switch>
// when none of the above match, <NoMatch> will be rendered- How ?
iterate over all of its childrenelements and only render the first one that matches the current location. - Props:
component
,render
, andchildren
- How ?
3. navigation components
<Link>
<NavLink>
:a special type of that can style itself as “active” when its ‘to’ prop matches the current location.<Redirect>
: navigate using its ‘to’ prop- 注意:不是 “函数”,而是“组件”,需要放在render中return
- 没有路由解析时候,可以直接使用redirect,重定向到默认页面
II. 3种方式renders Component
<Route component>
<Route render>
<Route children>
III. Route在render component同时,传入3个量,作为component的props(不论3中方式的哪一种)
- match
- history
- location
比如render:func( )方式render conponent
1 | // eg1: |
IV. Route vs Component如何关联
- Router 将路径 match上之后,会调用相应的 Route, 由Route 将相关的参数:match、location、history等传入到 Component中
- Route 的 state: match、location 传入到 Component的 props: match、location
- The Route will render
, where props are some router specific things that look like { match, location, history }. - If the user is not at /dashboard then the Route will render null. That’s pretty much all there is to it.
V. match.url
vs match.path
A match object contains information about how a
matched the URL.
- match:是
route
的state
;是component
的props
- Eg: consider the route
/users/:userId
match.path
- (string) 用于匹配路径模式。用于构建嵌套的; match.path
would be/users/:userId
match.url
- (string) URL 匹配的部分。 用于构建嵌套的 ;match.url
would have the :userId value filled in, e.g. “users/5”.”
VI. match.params
表示 url 匹配上的变量值
1 | <Route path="/:id" component={Child} /> |
URL 后半部分由变量 id 兜住,如果进来的url为/abc
,那么 match.params.id = abc
VII. Eg: Custom Link
Route实际上是 Component 的买办,为Component办事!
Route放在哪个位置,那里就有可能render出它携带的component(只要url match)
1 | <Route path="/abc" component={Child_1} /> |
VIII. <Prompt>
:防止中途跳转
适用情况:用户在填表过程中,点击了“后退”或“跳转链接”,需弹窗询问
<Prompt>
是保留tag,跟<Link>
一样
- 创建一个wrapper,包含
<form>
,<Prompt>
- wrapper中设置
state.inMiddle
,来决定是否render<Prompt>
- 改变
state.inMiddle
的根源:form
中的input
输入框;- 只要有任何输入值,就将
state.inMiddle
置为true
- 提交form后,将form reset()之后,需要将
state.inMiddle
置为false
- 只要有任何输入值,就将
Prompt
的props:when
:检查state.inMiddle
message
: 决定弹窗的显示信息
1 | class Form extends React.Component { |