A router library is no good if we have to hardcode every single route in our application. In this lesson we look at how to access variables in our routes and pass them into our components.

Define a route param by using ":message", () make it optional:

  1. <Route path="/(:message)" component={Home}></Route>

Get route param:

  1. const Home = (props) => <div><h1>{props.params.message ||'hello'}</h1><Links></Links></div>;

---------------

  1. import React from 'react';
  2. import {hashHistory, Route, Router, Link, IndexRoute} from 'react-router';
  3.  
  4. const Home = (props) => <div><h1>{props.params.message ||'hello'}</h1><Links></Links></div>;
  5.  
  6. const Links = () =>
  7. <nav >
  8. <Link to="/">Home</Link>
  9. <Link to="/foo">Foo</Link>
  10. <Link to="/bar">Bar</Link>
  11. </nav>;
  12.  
  13. class App extends React.Component {
  14. render(){
  15. return(
  16. <Router history={hashHistory}>
  17. <Route path="/(:message)" component={Home}></Route>
  18. </Router>
  19. );
  20. }
  21. }
  22.  
  23. export default App;

[React] React Router: Route Parameters的更多相关文章

  1. [React] React Router: Querystring Parameters

    Define query param in Link, accept path and query : const Links = () => <nav > <Link to= ...

  2. [React] React Router: Router, Route, and Link

    In this lesson we'll take our first look at the most common components available to us in react-rout ...

  3. [React] React Router: Redirect

    The Redirect component in react-router does exactly what it sounds like. It allows us to redirect fr ...

  4. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

  5. [React] React Router: IndexRoute

    IndexRoute allows us to define a default child component to be rendered at a specific route when no ...

  6. [React] React Router: Nested Routes

    Since react-router routes are components, creating nested routes is as simple as making one route a ...

  7. React+React Router+React-Transition-Group实现页面左右滑动+滚动位置记忆

    2018年12月17日更新: 修复在qq浏览器下执行pop跳转时页面错位问题 本文的代码已封装为npm包发布:react-slide-animation-router 在React Router中,想 ...

  8. ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例

    工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...

  9. Nginx支持 React browser router

    修改nginx配置文件,添加try_file配置如下,即可实现对 React browser router 的支持. location / { root /var/www/mysite; try_fi ...

随机推荐

  1. asp.net+Sqlserver 通过存储过程读取数据

    Sqlserver代码  创建存储过程如下: /*根据父id获取类别总数*/ IF EXISTS (SELECT name FROM sysobjects WHERE name = N'getsite ...

  2. MongoDB数据库导出导入迁移

    在server 1导出 /usr/local/mongodb/bin/mongorestore -d adv_new /tmp/mongodb/ 然后会在/tmp/mongodb/目录下生成一个adv ...

  3. 安卓手机内外SD卡互换

    相信有許多人....有內置sd太小...外置sd(sdcard2或extsd)卻只能放資料.... 一些遊戲或者是影音播放軟體....根本不會去讀外置sd(sdcard2或extsd)..... 記憶 ...

  4. 你好,C++(22) 排排坐,吃果果——4.3.3 for循环:某个范围内…每个都…

    4.3.3  for循环:某个范围内…每个都… 既然while语句和do…while…语句都已经可以满足我们表达循环现象的需要,那为什么C++还要专门提供for语句来表达循环现象呢?在现实世界中,常常 ...

  5. Yandex 2013Q(Atoms: There and Back Again-贪心+模拟+List)

    Atoms: There and Back Again Time limit 2 seconds Memory limit 256Mb Input stdin Output stdout Legend ...

  6. MySQL如何执行关联查询

    MySQL中‘关联(join)’ 一词包含的意义比一般意义上理解的要更广泛.总的来说,MySQL认为任何一个查询都是一次‘关联’ --并不仅仅是一个查询需要到两个表的匹配才叫关联,索引在MySQL中, ...

  7. 2.1 Java I/O简史

    Java 1.0 到 1.3 中的 IO 没有而 Java 1.4 中引入的 NIO 有的“改进”:非阻塞IO.缓冲区.通道层.字符集.内存数据.Perl(正则表达式之王): 下一代 I/O-NIO. ...

  8. JQUERY1.9学习笔记 之基本过滤器(一) 动态选择器

    动态选择器:animated Selector 描述:当选择器运行时,选择动态进程中的所有元素.(对动态进程起作用) jQuery( ":animated" ) 注释::anima ...

  9. 猜数字-js

    var n = Math.round(Math.random()*10); //随机数 // alert(n); while(true){ var Onum = prompt('请输入1-10之间的数 ...

  10. 转载自php100中文网 centos下lamp 环境搭建

    学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我介绍一下LAMP环境的搭建,即Linux.Apache.MySQL.PHP环境. 一. ...