React Router中NamedComponent与Params使用

前端之家收集整理的这篇文章主要介绍了React Router中NamedComponent与Params使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文从属于笔者的React入门与最佳实践系列,是对于React Router最新指南与异步加载实践的补充

NamedComponent

在正常的Route项配置中<Route path="messages/:id" component={Message} />,在component这个Prop中我们传入的是某个组件名,而如果我们需要在路由配置时动态构造一些组件,譬如我们希望传入标题HelloWorldMessage组件时,我们会需要以下配置:

  1. const TitledMessage = ()=>{<Message title="HelloWorld" /> }

这里是传入了一个函数式声明的组件,关于函数式声明的组件可以参考React中函数式声明组件。。而React Router官方也提供了我们另一种动态构造组件的方式,即是所谓的NamedComponent,即允许在子路由声明时将Props值作为路由配置参数传入父路由,譬如我们创建了需要传入两个Props参数的组件:

  1. const NamedComponents = (props) => (
  2. <div>
  3. {props.title}<br />
  4. {props.subTitle}
  5. </div>
  6. )

而NamedComponent需要的两个输入参数其实也是两个可组合的组件:

  1. const Title = () => (
  2. <h1>Hello from Title Component</h1>
  3. )
  4. const SubTitle = () => (
  5. <h1>Hello from SubTitle Component</h1>
  6. )

然后我们可以为NamedComponent组件创建一个单独的路由,而其默认子路由可以是两个组件:

  1. <Route path='/namedComponent' component={NamedComponents}>
  2. <IndexRoute components={{ title: Title,subTitle: SubTitle }} />
  3. </Route>

最后的效果如下所示:

Params

Route Parameters

很多应用的不可或缺的一个点就是从URL中读取路由参数,React Router自然也为我们提供了路由参数设置与读取的功能,譬如在定义路由时,我们可以直接将参数定义入路由中:

  1. <Route path='/about/:name' component={About} />

而在需要读取该参数的组件中:

  1. const About = (props) => (
  2. <div>
  3. <h3>Welcome to the About Page</h3>
  4. <h2>{props.params.name}</h2>
  5. </div>
  6. )

有时候,我们也需要设置可选参数,譬如如果按照上面的配置方案我们直接访问/#/about是无法访问到About组件的,有点类似于正则表达式的写法:

  1. <Route path='/about(/:name)' component={About} />

另有一个小Trick,有时候我们在组件内部展示元素的时候是需要根据是否有参数传入然后再判断是否需要显示:

  1. { props.params.name && <h2>Hello,{props.params.name}</h2>}

Query String Parameters:查询参数

上文介绍的是按照路由参数的方式进行参数传递,就像HTTP URL标准一样,有时候我们也需要按照查询参数的方式来进行参数传递,譬如我们定义了如下基于查询参数的组件:

  1. const Query = (props) => (
  2. <h2>{props.location.query.message}</h2>
  3. )

然后我们需要在刚才构建好的路由地址中添加上该组件:

  1. ...
  2. <Route path='/address' component={Address}>
  3. <IndexRoute component={TwitterFeed} />
  4. <Route path='instagram' component={Instagram} />
  5. <Route path='query' component={Query} />
  6. </Route>
  7. ...

而对应的带参数传递的跳转到该组件的Link为:

  1. <IndexLink
  2. activeClassName='active'
  3. to={{
  4. pathname: '/address/query',query: { message: 'Hello from Route Query' }
  5. }}>Route Query</IndexLink>

最后,我们在官方的总的例子中来看下两种参数的使用:

  1. import React from 'react'
  2. import { render } from 'react-dom'
  3. import { browserHistory,Router,Route,Link } from 'react-router'
  4. import withExampleBasename from '../withExampleBasename'
  5. const User = ({ params: { userID },location: { query } }) => {
  6. let age = query && query.showAge ? '33' : ''
  7. return (
  8. <div className="User">
  9. <h1>User id: {userID}</h1>
  10. {age}
  11. </div>
  12. )
  13. }
  14. const App = ({ children }) => (
  15. <div>
  16. <ul>
  17. <li><Link to="/user/bob" activeClassName="active">Bob</Link></li>
  18. <li><Link to={{ pathname: '/user/bob',query: { showAge: true } }} activeClassName="active">Bob With Query Params</Link></li>
  19. <li><Link to="/user/sally" activeClassName="active">Sally</Link></li>
  20. </ul>
  21. {children}
  22. </div>
  23. )
  24. render((
  25. <Router history={withExampleBasename(browserHistory,__dirname)}>
  26. <Route path="/" component={App}>
  27. <Route path="user/:userID" component={User} />
  28. </Route>
  29. </Router>
  30. ),document.getElementById('example'))

猜你在找的React相关文章