Since react-router routes are components, creating nested routes is as simple as making one route a child of another in JSX.

Make the nested component:

class App extends React.Component {
render(){
return(
<Router history={hashHistory}>
<Route path="/" component={Home}>
<Route path="about" component={About}>
<Route path="contact" component={Contact}></Route>
</Route>
</Route>
</Router>
);
}
}

Change the path:

const Links = () =>
<nav >
<Link activeStyle={{color: 'green'}} to="/">Home</Link>
<Link activeStyle={{color: 'green'}} to="/about">About</Link>
<Link activeClassName="active" to="/about/contact">Contact</Link>
</nav>;

Pass the props.children to the nested component:

const Home = (props) => <div><h1>Home</h1><Links></Links>{props.children}</div>;
const About = (props) => <div><h1>About</h1>{props.children}</div>;
const Contact = () => <div><h1>Contact</h1></div>;

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

import React from 'react';
import {hashHistory, Route, Router, Link} from 'react-router'; // About is the child of home, to display the about, we need to access
// props.children
const Home = (props) => <div><h1>Home</h1><Links></Links>{props.children}</div>;
const About = (props) => <div><h1>About</h1>{props.children}</div>;
const Contact = () => <div><h1>Contact</h1></div>; const Links = () =>
<nav >
<Link activeStyle={{color: 'green'}} to="/">Home</Link>
<Link activeStyle={{color: 'green'}} to="/about">About</Link>
<Link activeClassName="active" to="/about/contact">Contact</Link>
</nav>; class App extends React.Component {
render(){
return(
<Router history={hashHistory}>
<Route path="/" component={Home}>
<Route path="about" component={About}>
<Route path="contact" component={Contact}></Route>
</Route>
</Route>
</Router>
);
}
} export default App;

[React] React Router: Nested Routes的更多相关文章

  1. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

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

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

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

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

  4. Nginx支持 React browser router

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

  5. react 装 router - yarn add react-router-dom@next

    react 装 router yarn add react-router-dom@next

  6. [React] react+redux+router+webpack+antd环境搭建一版

    好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...

  7. react react使用css

    在react 中使用css有以下几种方法 第一种全局使用 app.js import React from 'react'; import Router from "./router&quo ...

  8. React/React Native 的ES5 ES6写法对照表

    //es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...

  9. React/React Native 的ES5 ES6写法对照表-b

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

随机推荐

  1. memcache和数据库的使用技巧

    1.加速无数据的访问速度毋庸置疑取数据先去取下memcache里的数据,如果没有再去数据库取数据但这样如果我取100次都是没有的那么我得去数据库去取100次 如果还是重复的...那么效率就不高了 解决 ...

  2. CentOS安装JDK7

    1.下载jdk-7-linux-x64.rpm(http://www.Oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-downlo ...

  3. 你好,C++(27)在一个函数内部调用它自己本身 5.1.5 函数的递归调用

    5.1.5 函数的递归调用 在函数调用中,通常我们都是在一个函数中调用另外一个函数,以此来完成其中的某部分功能.例如,我们在main()主函数中调用PowerSum()函数来计算两个数的平方和,而在P ...

  4. cookie,localStorage,sessionStorage

    1.cookie 由于HTTP是一种无状态的协议,服务器单从请求内容上无法判断客户身份.因此,cookie弥补这个缺陷,每次请求都携带cookie,这样服务器就能能知道不同的客户端请求了. 对于同一域 ...

  5. combobox只读代码

    public partial class Form1: Form { // combobox只读代码 [DllImport("user32.dll", CharSet = Char ...

  6. 【PHP】新浪、淘宝的地区 API调用

    /推荐使用新浪的: $ch = curl_init(); $url = "http://ip.dpool.sina.com.cn/iplookup/iplookup.php?format=j ...

  7. MOOTOOLS和JQUERY如何同时存在,解决冲突

    mootools-jquery 今天在做EcStore前台的做效果时,由于Jquery的插件比较多,于是就使用了Jquery的插件,但是发现会引起Mootools的冲突. 于是猛找资料,终于找到了,现 ...

  8. KACK的处理方法

    demo: .eq { color:#f00;/*标准浏览器*/ color:#f30\0;/*IE8,IE9,opera*/ *color:#c00;/*IE7及IE6*/ _color:#600; ...

  9. Flask源码阅读笔记(一)

    作者:acezio链接:https://zhuanlan.zhihu.com/p/21358368来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. flask的url r ...

  10. 汉诺塔 Hanoi Tower

    电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...