[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),然而网上搜到的很多教 ...
随机推荐
- vs2012加载EntityFrameWork框架,连接Oracel
近日公司用到.net MVC框架做接口,需连接到Oracel数据库,从网上查阅了一些资料,当然,从咱们博客园获益匪浅.然后结合自己所做,把使用过程中遇到的一些问题,及如何解决的整理如下,方便查阅,也有 ...
- C# Gma.QrCodeNet生成二维码
一.使用C#生成二维码类库之一是qrcodenet 源代码地址: http://qrcodenet.codeplex.com/ 二.使用实例 定义处理字符串 static string url = & ...
- HTML中常用鼠标样式
语法:cursor : auto | all-scroll | col-resize| crosshair | default | hand | move | help | no-drop | not ...
- MongoDB与PHP的添加、修改、查询、删除
链接数据库使用下面的代码创建一个数据库链接 <?php$connection = new Mongo(); //链接到 localhost:27017$connection = new Mong ...
- PHP FTP
安装 PHP 的 Windows 版本内置了对 FTP 扩展的支持.无需加载任何附加扩展库即可使用 FTP 函数. 然而,如果您运行的是 PHP 的 Linux 版本,在编译 PHP 的时候请添加 - ...
- 自定义分页标签,并使分页标签能获得url中的参数
如题,要实现一个分页功能,其次,要让分页标签“智能一点”,在分页时能自动带上url后面的参数 <tag> <description>分页标签</description&g ...
- 转载:Linux的vim三种模式
一般模式:在Linux终端中输入“vim 文件名”就进入了一般模式,但不能输入文字. 编辑模式:在一般模式下按i就会进入编辑模式,此时就可以写程式,按Esc可回到一般模式. 命令模式:在一般模式下按: ...
- 向null地址copy数据和不断改变指针指向
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...
- CSS 3层嵌套居中布局
<html> <head> <style type="text/css"> .root{ background-color: red; widt ...
- 个人vim配置(.vimrc文件分享)
syntax enable syntax on colorscheme desert set nu! set nowrap set nobackup set backspace= set tabsto ...