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 ...
随机推荐
- JS练习题
1.带参数的函数不能重复调用 2.顺序语句: 3.在重复调用中打断 调用用 return 例如: Interval 时间间隔 先清一下 再接着 return:其他的调用也可以加return打 ...
- 批处理命令——goto 和 :
谈起goto,相信大家应该想到的是面向过程编程.其实,这就相当于当有人向你谈起class,意味着你就懂得面向对象编程.如果你不懂,那么你们的沟通将会很困难.不懂我说的啥意思吗?请参见曾经分享王路的一篇 ...
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 【leetcode】Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- jquery $(document).ready() 与window.onload的异同
Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 ...
- Spring面向切面编程(AOP)方式二
使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...
- AutoLayout 图解各种约束
- chrome进入控制台时自动进入断点模式的解决方法
简单粗暴,不知道为什么,去掉那个√就好了
- Javascript异步编程方法总结
现在我们有三个函数,f1, f2, f3 按正常的思路我们会这样写代码: function f1 (){}; function f2 (){}; function f3 (){}; //在这里调用函数 ...
- mysql常用表/视图管理语句
查看所有表 show tables; 查看表/视图结构 desc 表名/视图名: 查看建表过程 show create table 表名: 查看建视图过程 show create view 视图名 ...