[React] React Router: Nested Routes
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的更多相关文章
- [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 ...
- React+React Router+React-Transition-Group实现页面左右滑动+滚动位置记忆
2018年12月17日更新: 修复在qq浏览器下执行pop跳转时页面错位问题 本文的代码已封装为npm包发布:react-slide-animation-router 在React Router中,想 ...
- ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例
工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...
- Nginx支持 React browser router
修改nginx配置文件,添加try_file配置如下,即可实现对 React browser router 的支持. location / { root /var/www/mysite; try_fi ...
- react 装 router - yarn add react-router-dom@next
react 装 router yarn add react-router-dom@next
- [React] react+redux+router+webpack+antd环境搭建一版
好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...
- react react使用css
在react 中使用css有以下几种方法 第一种全局使用 app.js import React from 'react'; import Router from "./router&quo ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
随机推荐
- 无法连接vCenter Server清单https://IP:10443
VMware vCenter Server服务器安装系统的时候使用一个IP,安装完VMware vCenter后来更换了另外一个IP,当使用vSphere Web Client登陆VMware vCe ...
- Nagios配置—添加linux主机监控
nagios安装请参看:Nginx平台安装Nagios监控服务 下面是我添加linux监控机的过程,如有错误或者不当的地方请指出: 测试环境: 监控主机:nagios+nagios插件+nrpe+网站 ...
- Cacti安装教程
CentOS 6.0架设流量监控及集中日志系统 第一章.cacti的安装 1. 系统的基本设置2. 设置主机名3. [root@localhost ~]# vi /etc/sysconfig/netw ...
- libusb简介
概述 libusb是一个C库,它提供了通用的访问USB设备. 它的目的是供开发人员使用方便的生产与USB通信硬件的应用程序. 可移植的: 使用一个跨平台API,它提供了访问USB设备在Linux上,O ...
- yum 安装 kvm
KVM是什么? for Kernel-based Virtual Machine 安装环境Centos 6.5 mini x64 禁用selinux, 禁用防火墙 安装过程: 1. 查看我们的电脑是 ...
- javascript的navigator对象
navigator 对象 转载: http://www.itlearner.com/code/js_ref/brow1.htm 包含了正在使用的 Navigator 的版本信息. 客户端对象 实现 ...
- JBPM4.4GPD设计器中文乱码问题的另一种解决方法
在eclipse中使用JBPM4.4的设计器时,输入中文后直接查看Source发现xml里中文全都乱码了,这时候大约整个人都不好了!赶紧百度.谷歌,搜到的多数结果都是要你在eclipse.ini或my ...
- C程序设计语言练习题1-15
练习1-15 重新编写1.2节中的温度转换程序,使用函数实现温度转换计算. 代码如下: #include <stdio.h> // 包含标准库的信息. float fahrToCelsiu ...
- 对比iOS网络组件:AFNetworking VS ASIHTTPRequest
对比iOS网络组件:AFNetworking VS ASIHTTPRequest 作者 高嘉峻 发布于 2013年2月28日 | 7 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件 ...
- 指定Action、Category调用系统Activity
1.Intent对象详解 Android的应用程序包含三种重要组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来 ...