欢迎大家指导与讨论 : )

  一、前言

    本文摘要:react-router的基本用法动画效果与路由,路由权限控制,路由离开确认,根据路由切分的按需加载,路由组件的属性。本文是笔者在学习时整理的笔记,由于技术水平还不够高,有错误的地方恳请各位指出,共同进步O(∩_∩)O

  二、基本用法

    二、1 路由定义(摘要: Router, Route)

      在一个路由系统中,我们需要定义好路由表。我们需要用Router组件包含我们的路由表,通过Route来声明单个的路由。同时,每个路由应该与它所属的组件一一对应

render((
<Router >//开始创建路由表
<Route path="/" component={App}>//声明每一个路由
<Route path="/about" component={About}/>
<Route path="users" component={Users}>//每个路由
<Route path=":id" component={User}/>//对应一个Component
</Route>
</Route>
</Router>
), document.getElementById('example')) //其他定义路由表的方法

import routes from './config/routes'

render(<Router history={browserHistory} routes={routes}/>, document.getElementById('example'))

    二、2 路由嵌套(摘要: IndexRouter, this.props.children)

      如何实现路由的嵌套呢,与Angular路由一样,路由的嵌套需要两个条件。一、在创建路由表的时候就声明好嵌套的规则(ng中的$stateProvider);二、需要有一个view来安放所要嵌套的子页面(ng中的ui-view)。其中,我们还可以在嵌套层的首个路由声明中,使用IndexRoute来声明该路由下的默认路由(类似于ng中$urlProvider.otherwise)

//有嵌套与默认页面的路由表
render((
<Router >
<Route path="/" component={App}>
<IndexRoute component={Index}/>//设置默认页面
<Route path="/about" component={About}/>
<Route path="users" component={Users}>
<IndexRoute component={UsersIndex}/>//设置默认页面
<Route path=":id" component={User}/>
</Route>
</Route>
</Router>
), document.getElementById('example')) //一个用于安放子页(子路由)的view
class Users extends React.Component {
render() {
return (
<div>
<h2>Users</h2>
{this.props.children}//此处相当于<ui-view>
</div>
)
}
}

    二、3 路由跳转(摘要: Link,to)

      我们需要一个Link组件帮助我们实现路由的的跳转  <li><Link to="/users" >/users</Link></li> ,最终Link组件会被渲染为<a>标签。to属性是我们所要跳转的路由pathname(类似于ng中的ui-sref / href)

      二、3 . 1 父子路由的参数穿透传递(摘要: to = {`/xx/${xxx}/`} )

        若父路由中包含不确定参数,而我们又想把该参数往下级传递,这时候我们需要这样子做

//路由配置
<Route path="user/:userID" component={User}>
<Route path="tasks/:taskID" component={Task} />
<Redirect from="todos/:taskID" to="tasks/:taskID" />
</Route>
//子级路由
<li><Link to={`/user/${userID}/tasks/foo`} activeClassName="active">foo task</Link></li>

      二、3 . 2 带参数的路由跳转

<li><Link      to={{ pathname: '/users/ryan', query: { foo: 'bar' } }} activeStyle={ACTIVE}>/users/ryan?foo=bar</Link></li>

      二、3 . 3 函数内跳转(摘要: this.context.router.push('/'))

          this.context.router.push('/') ,注:这个写法会把跳转载入浏览器历史,若不想留下历史记录则可以 this.context.router.replace('/')

  三、带有动画效果的路由切换(摘要: ReactCSSTransitionGroup)

    当我们需要在路由切换时带有一定的动画效果时,我们便需要 react-addons-css-transition-group 这个插件了。使用ReactCSSTransitionGroup组件来包含我们需要呈现动画效果的view

class App extends Component {
render() {
return (
<div>
<ul>
<li><Link to="/page1">Page 1</Link></li>
<li><Link to="/page2">Page 2</Link></li>
</ul> <ReactCSSTransitionGroup component="div" transitionName="example" transitionEnterTimeout={500} transitionLeaveTimeout={500}
>
{React.cloneElement(this.props.children, {
key: this.props.location.pathname
})}
</ReactCSSTransitionGroup>
//克隆所有子节点,单独的{this.props.children}没有动画效果
</div>
)
}
}

  四、路由的权限控制(摘要: onEnter、context.router)

    单页应用路由的权限控制的基本思路是:监听路由的改变,每当路由将要发生改变,我们就使用一个中间服务(该服务介于上一级路由和将要到达路由之间启动),来判断我们是否有进入这个路由的权限,有的话直接进入,没有的话就redirect。在React中,为某个路由进行权限监听的方式是onEnter <Route path="page" component={Page} onEnter={requireCredentials}/> ,该onEnter属性对应连着一个具有判断权限的中间服务。我们通过上一级路由来启动这个服务。假设我们需要从'/form'到'/page'之间做一个判断,在'/form'中填写特定字段后才能成功跳转,否则redirect到'/error'

//form
const Form = createClass({
//省略部分代码
submitAction(event) {
event.preventDefault();
//通过context传输数据
//通过url的query字段传输数据
//也可以通过制定其他服务来传输数据
this.context.router.push({
pathname: '/page',
query: {
qsparam: this.state.value
}
})
},
render() {
return (
<form onSubmit={this.submitAction}>
//省略部分代码
<button type="submit">Submit </button>
</form>
)
}
}) //路由权限控制
<Route path="page" component={Page} onEnter={requireCredentials}/> //权限控制的中间服务
function requireCredentials(nextState, replace, next) {
//获取传输过来的数据
if (query.qsparam) {
serverAuth(query.qsparam)
.then(
() => next(),//成功,通过next()成功跳转
() => {
replace('/error')//重定向
next()
}
)
} else {
replace('/error')
next()
}
}  

     其中,onEnter所指向的函数是 type EnterHook = (nextState: RouterState, replace: RedirectFunction, callback?: Function) => any; 其中,nextState作为第一个参数,其所带的信息有如下:

type RouterState = {
location: Location;
routes: Array<Route>;
params: Params;
components: Array<Component>;
};

      其中,replace函数一旦被使用到,则在函数内部跳转到一个新url,返回时也要带上必要的信息,如下

type RedirectFunction = (state: ?LocationState, pathname: Pathname | Path, query: ?Query) => void;

   六、路由离开确认(摘要: componentWillMount, this.context.router.setRouteLeaveHook) 

     若我们需要在路由切换,在离开当前页面的时候做一些确认工作,我们可以通过setRouteLeaveHook函数,为离开前执行一些操作

//Component内部
componentWillMount() {
this.context.router.setRouteLeaveHook(
this.props.route,
this.routerWillLeave
)
} routerWillLeave() {
if (xxx)
//...
},

  七、根据路由按需加载组件

    按需加载在单页应用中的好处不言而喻,按业务模块切分代码能使首次加载资源所需要的时间大大降低,能在一定程度上增强用户体验。但首先我们需要整理一下我们的项目结构(此demo是按路由切分的,另外还能按业务模块进行切分)

    七 . 1 项目结构

    七 . 2 路由表配置(app.js)

    七 . 3 对应组件的加载配置(routes/hello/index.js和routes/test/index.js)

  八、路由组件的属性(摘要: this.props)

  九、路由Location属性

type Location = {
pathname: Pathname;
search: QueryString;
query: Query;
state: LocationState;
action: Action;
key: LocationKey;
};

React-Router学习整理的更多相关文章

  1. React Router学习

    React Router教程 本教程引用马伦老师的的教程 React项目的可用的路由库是React-Router,当然这也是官方支持的.它也分为: react-router 核心组件 react-ro ...

  2. React Router学习笔记(转自阮一峰老师博客)

    React Router是一个路由库,通过管理URL来实现组件切换和状态转变. 1.安装和使用 $ npm install -S react-router 在使用时,作为React组件导入 impor ...

  3. React 入门学习笔记整理目录

    React 入门学习笔记整理(一)--搭建环境 React 入门学习笔记整理(二)-- JSX简介与语法 React 入门学习笔记整理(三)-- 组件 React 入门学习笔记整理(四)-- 事件 R ...

  4. React Router V4.0学习笔记

    最近在学习React Router,但是网站的教程多半还是3.X版本之前的,所以我只能在GitHub上找到React Router的官方文档在读.后来总结了一下,包括学习经验以及V3.X与V4.X的差 ...

  5. 初步学习React Router 4.0

      React Router 4.0 是react官方推荐的路由库.4是已经正式发布的最新版本. 初始化项目启动之后: npm run eject 弹出配置文件.自定义配置webpack 查看下pac ...

  6. React初识整理(四)--React Router(路由)

    官网:https://reacttraining.com/react-router 后端路由:主要做路径和方法的匹配,从而从后台获取相应的数据 前端路由:用于路径和组件的匹配,从而实现组件的切换. 如 ...

  7. React Redux学习笔记

    React Router React Router 使用教程 Redux中间件middleware [译]深入浅出Redux中间件 Redux学习之一:何为middleware? ES6 ES6新特性 ...

  8. React入门资源整理

    另外,附上我搜集的一些比较实用的学习资料,建议先看这些撸起来,再看什么乱七八糟的awsome系列. React入门资源整理 React项目新手指南 http://www.w3ctech.com/top ...

  9. React Router 4.x 开发,这些雷区我们都帮你踩过了

    前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...

  10. React Router API文档

    React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...

随机推荐

  1. javascript是判断对象是否是数组

    JS中的数据类型: 2大类 原始类型:值保存在变量本地的数据类型 5种:Number String Boolean undefined null Number:8bytes 舍入误差-->四舍五 ...

  2. 解决sea.js引用jQuery提示$ is not a function的问题

    在使用sea.js的如下写法引用jQuery文件时, //main.jsdefine(function(require,exports,module){ var $ = require('jquery ...

  3. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(十一)路径导航模块

    config.xml文件的配置如下: <widget label="路径导航" icon="assets/images/lujingdaohang.png" ...

  4. SharePoint 2013 图文开发系列之事件接收器

    在SharePoint的使用中,我们经常需要在完成一个动作之后,触发一个事件:比如,我们上传一个文档,但是没有标题,我们需要在上传完成之后,触发一个事件把文件名同步到标题,这就需要用到事件接收器. 此 ...

  5. SharePoint 2007 User Re-created in AD with new SID issue on MySite

    When active directory users get deleted and re-created, even with the same user id, there's a nasty ...

  6. 解决:eclipse删除工程会弹出一个对话框提示“[project_name]”contains resources that are not in sync with"[workspace_name...\xx\..xx\..\xx]"

    提示“[project_name]”contains resources that are not in sync with"[workspace_name...\xx\..xx\..\xx ...

  7. 【原】iOS:手把手教你发布代码到CocoaPods(Trunk方式)

    Change Log: 2015.08.20 - 添加podspec文件更新方法 2015.08.19 - 首次发布 概述 关于CocoaPods的介绍不在本文的主题范围内,如果你是iOS开发者却不知 ...

  8. SDWebImage原理及使用(转)

    转自http://www.cnblogs.com/jys509/p/5199997.html SDWebImage托管在github上.https://github.com/rs/SDWebImage ...

  9. ORA-12520: TNS:listener could not find available handler for requested type of server

    当你碰到ORA-12520错误时,如下所示: 英文错误提示: ORA-12520: TNS:listener could not find available handler for requeste ...

  10. 解决问题:The context cannot be used while the model is being created

    使用Entity Framework (v6.1.3)突然遇到这个问题了,之前一直好好的,怎么破? 此处省略了多次在“好”与“坏"的项目中试验的过程(苦啊),直接给出答案.答案是:没有按Db ...