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 ...
随机推荐
- codeforces 490C. Hacking Cypher 解题报告
题目链接:http://codeforces.com/problemset/problem/490/C 题目意思:给出一个可能有10^6 位长的字符串且没有前导0的整数,问能否一分为二,使得前面的一部 ...
- HDU 4950 Monster
题目链接 题意:有一个怪物的血量为h,人攻击怪物,每回合可以杀掉a滴血,再回b滴血,k个回合之后人会休息一回合,即人不攻击而怪物回b滴血,问能否杀死.翻译过来就是给定一个数h,每轮可以先减a再加b,k ...
- HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...
- 20145213《Java程序设计》实验五Java网络编程及安全
20145213<Java程序设计>实验五Java网络编程及安全 实验内容 1.掌握Socket程序的编写. 2.掌握密码技术的使用. 3.设计安全传输系统. 实验预期 1.客户端与服务器 ...
- 苹果的软件/系统盘 网站 http://www.panduoduo.net/u/bd-369186934/2
http://www.panduoduo.net/u/bd-369186934/2
- October 7th 2016 Week 41st Friday
The land didn't move, but moved; the sea was not still, yet was still. 大地止而亦行,大海动而亦静. Remember that ...
- 搞笑世界杯(codevs 1060)
题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...
- NYOJ题目611练练
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJ1CAIAAACgqiqJAAAgAElEQVR4nO3du27jSp4HYL+Ecj2IYz
- webservice 简单入门 (NLY)
1,创建webservice服务器端 搭建网站,创建webservice webservice.cs中的代码 namespace WebApplication1 { /// <summary&g ...
- poj 1005:I Think I Need a Houseboat(水题,模拟)
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 85149 Acce ...