react-router
基本的构建
import ReactRouter from 'react-router';
let {Route, Router, Link, IndexRoute} = ReactRouter.Route;
.....
let Nav = React.createClass({
render () {
return (
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/inbox">Inbox</Link></li>
</ul>
</div>
)
}
});
let App = React.createClass({
render () {
return (
<div>
<h1>App</h1>
<Nav />
{this.props.children || || 'welcome to app'}
</div>
)
}
});
React.render((
<Router>
<Route path='/' component={App}>
<IndexRoute component={Home}/>
<Route path='about' component={About}/>
<Route path='inbox' component={Inbox}/>
</Route>
</Router>
), document.body);
嵌套组件结构
let Inbox = React.createClass({
render () {
return (
<div>
<h3>inbox</h3>
{this.props.children || "Welcome to your Inbox"}
</div>
)
}
});
let Message = React.createClass({
render() {
var id = this.props.params.id;
var datas = ['zero','one','two','three','four','five'];
var data = datas[id] || 'none';
var links = datas.map(function (data, index){
return (<li key={index}><Link to={`/messages/{index}`} >{index}</Link></li>)
})
return (
<div>
{links}
<h3>{data}</h3>
</div>
)
}
})
React.render((
<Router>
<Route path="/" component={App}>
.....
<Route path="inbox" component={Inbox}>
<Route path="messages/:id" component={Message} />
</Route>
</Route>
</Router>
), document.body)
整体组件的结构
| URL | Components |
|---|---|
/ |
App-> Home |
/about |
App -> About |
/inbox |
App -> Inbox |
/inbox/messages/:id |
App -> Inbox -> Message |
URL和结构
<link/>结构:<Link to="/inbox/messages/:id">Message</Link>如果想在
url上解耦,结构上仍然成为子组件,改成
<Route path="inbox" component={Inbox}>
<Route path="/messages/:id" component={Message} />
</Route>
//
<Link to="/messages/:id">Message</Link>
- 解耦后想把
/inbox/messages/:id的连接跳转到/messages/:id;
<Route path="inbox" component={Inbox}>
<Route path="/messages/:id" component={Message} />
<Redirect from="messages/:id" to="/messages/:id" />
</Route>
钩子
onEnter, onLeave
<Route path='about' component={About} onEnter={redirectToChild}/>
//
function redirectToChild(nextState, redirectTo, callBack) {
redirectTo('/about', '', {nextPathname: nextState.location.pathname});
}
- 钩子函数结构
//EnterHook
type EnterHook = (nextState: RouterState, replaceState: RedirectFunction, callback?: Function) => any;`
type RouterState = {
location: Location;
routes: Array<Route>;
params: Params;
components: Array<Component>;
};
type RedirectFunction = (pathname: Pathname | Path, query: ?Query, state: ?LocationState) => void;
//LeaveHook
type LeaveHook = () => any;
路径匹配
<Route path="/hello/:name"> // matches /hello/michael and /hello/ryan
<Route path="/hello(/:name)"> // matches /hello, /hello/michael, and /hello/ryan
<Route path="/files/*.*"> // matches /files/hello.jpg and /files/path/to/hello.jpg
与上一版的具体变化
传入组件的参数
- 在配置好路径后,
<Route>组件会将一下参数传入this.props
history: Object
- 常用
history.pushState():来改变当前路径;
location: Object
- 当下路径解析后的参数对象;
params: Object
- 当前路径的
param值;
route: Object
routeParams: Object
routes: Array
react-router的更多相关文章
- [Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components ...
- [转] React Router 使用教程
PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...
- [Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- [Redux] Adding React Router to the Project
We will learn how to add React Router to a Redux project and make it render our root component. Inst ...
- React Router基础使用
React是个技术栈,单单使用React很难构建复杂的Web应用程序,很多情况下我们需要引入其他相关的技术 React Router是React的路由库,保持相关页面部件与URL间的同步 下面就来简单 ...
- 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)
请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...
- react router 4.0以上的路由应用
thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...
- React Router 使用教程
一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...
- 关于react router 4 的小实践
详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...
- React Router 4.x 开发,这些雷区我们都帮你踩过了
前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...
随机推荐
- div+css进度条
效果图: 进度条代码: <style type="text/css"> 红色:background-color:f05153:border:1px solid #f05 ...
- 【linux】学习4
文件压缩: gzip :压缩 解压缩 zcat: 读取压缩文件 gzip text1 :压缩text1 得到 text1.gz 原文件不见了 gzip -c text1 > text1.g ...
- MFC CheckBox
if ( BST_CHECKED == IsDlgButtonChecked( IDC_CHECK1 ) ){// 勾选}else{}
- IntelliJ IDEA 15.0.4常用快捷键整理
一.背景 最近刚转了IDEA,感觉真是爽的一逼,太智能了,回不去Eclipse了,还有些淡淡的忧伤呢~在使用中很多的快捷键帮了开发的大忙,让我可以达到事半功倍的效果,下面就罗列出来,与大家共同分享. ...
- Java反射实战
一.背景 最近的项目中需要使用到Java 反射的知识,以前不怎么了解,也基本没怎么用过,抽出一片时间,来具体学习和实战下Java的反射!拿来和大家分享以及记录方便以后学习! 二.反射相关概念解析 1. ...
- August 21st 2016 Week 35th Sunday
I figure life is a gift and I don't intend on wasting it. 我觉得生命是一份礼物,我不想浪费它. Tonight when I was runn ...
- Android 录音
想要实现wav格式的编码时我们也就不能再使用MediaRecorder,而只能使用AudioRecord进行处理
- iOS - property,strong,weak,retain,assign,copy,nomatic 的区别及使用
1:ARC环境下,strong代替retain.weak代替assign,xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的.xcod ...
- 6个原因说服你选择PostgreSQL9.6
PostgreSQL9.6在前些日子发布了, 社区为该版本的重大更新付诸良多, 发布日志一如既往的长,我挑选了6个重要的更新, 这些或许能够帮助你更好的使用PostgreSQL. 并行: 并行应该是这 ...
- Clr Via C#读书笔记---CLR寄宿和应用程序域
#1 CLR寄宿: 开发CLR时,Microsoft实际是将他实现成包含在一个dll中的COM服务器.Microsoft为CLR定义了一个标准的COM接口,并为该接口和COM服务器分配了GUID.安装 ...