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 ...
随机推荐
- ajax传值方式为数组
js: function responseJson1(){ var array=[1001,1002]; var str=""; //获取table对象 ...
- ListView之BaseAdapter的使用
话说开发用了各种Adapter之后感觉用的最舒服的还是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView.GridView.Gal ...
- (转)Delphi工程文件说明
1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...
- AutoLayout 图解各种约束
- [Android]在代码混淆中关闭 Log
-assumenosideeffects class android.util.Log{ public static *** d(...); public static *** e(...); }
- hdu1141(二进制数位,二分,打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1141 题意:××公司是制造computer的,1960年它造的computer是4bit的,之后每10 ...
- iOS开发-正则表达式的使用方法
前言:在表单验证中,我们经常会使用到正则,因为我们需要用它来判断用户输入的字符是否为合法的,如果是不合法的,那么应该提示用户输入错误,并不让提交至服务器.我们也可以通过正则表达式,从用户输入的字符串中 ...
- MVC基础知识 – 2.新语法
1.自动属性 Auto-Implemented Properties 2.隐式类型 var 3.参数默认值 和 命名参数 4.对象初始化器 与 集合初始化器 { } 5.匿名类 & 匿名方法 ...
- Redis不同数据类型的的数据结构实现
我们知道Redis支持五种数据类型, 分别是字符串.哈希表(map).列表(list).集合(set)和有序集合,和Java的集合框架类似,不同数据类型的数据结构实也是不一样的. >>Re ...
- Linux进程状态 ( Linux Process State Codes)
进程状态代码及说明: STATE代码 说明 D 不可中断的睡眠. 通常是处于I/O之中. R 运行中/可运行. 正处于运行队列中. S 可中断的睡眠. 等待某事件发生. T 已停止. 可能是因为she ...