# 1. 理解react-router
react的一个插件库
专门用来实现一个SPA应用
基于react的项目基本都会用到此库

# 2. 几个重要问题
## 1). SPA应用
单页Web应用(single page web application,SPA)
整个应用只有一个完整的页面
点击页面中的链接不会刷新页面, 本身也不会向服务器发请求
当点击链接时, 只会做页面的局部更新
数据都需要通过ajax请求获取, 并在前端异步展现
## 2). 路由
1. 什么是路由?
一个路由就是一个映射关系(key:value)
key为路由路径, value可能是function/component
2. 路由分类
后台路由: node服务器端路由, value是function, 用来处理客户端提交的请求并返回一个响应数据
前台路由: 浏览器端路由, value是component, 当请求的是路由path时, 浏览器端前没有发送http请求, 但界面会更新显示对应的组件
3. 后台路由
* 注册路由: router.get(path, function(req, res))
* 当node接收到一个请求时, 根据请求路径找到匹配的路由, 调用路由中的函数来处理请求, 返回响应数据
* 前端路由
* 注册路由: <Route path="/about" component={About}>
* 当浏览器的hash变为#about时, 当前路由组件就会变为About组件
## 3). 关于url中的#
1. 理解#
'#'代表网页中的一个位置。其右面的字符,就是该位置的标识符
改变#不触发网页重载
改变#会改变浏览器的访问历史
2. 操作#
window.location.hash读取#值
window.onhashchange = func 监听hash改变
3. 学习资源:
阮一峰教程: http://www.ruanyifeng.com/blog/2011/03/url_hash.html

# 3. react-router的学习资源
github主页: https://github.com/ReactTraining/react-router
官网教程: https://github.com/reactjs/react-router-tutorial
阮一峰教程: http://www.ruanyifeng.com/blog/2016/05/react_router.html

# 4. 相关API
## 1). react-router中的相关组件:
Router: 路由器组件, 用来包含各个路由组件
Route: 路由组件, 注册路由
IndexRoute: 默认子路由组件
hashHistory: 路由的切换由URL的hash变化决定,即URL的#部分发生变化
Link: 路由链接组件
## 2). Router: 路由器组件
属性: history={hashHistory} 用来监听浏览器地址栏的变化, 并将URL解析成一个地址对象,供React Router匹配
子组件: Route
## 3). Route: 路由组件
属性1: path="/xxx"
属性2: component={Xxx}
根路由组件: path="/"的组件, 一般为App
子路由组件: 子<Route>配置的组件
## 4). IndexRoute: 默认路由
当父路由被请求时, 默认就会请求此路由组件
## 5). hashHistory
用于Router组件的history属性
作用: 为地址url生成?_k=hash, 用于内部保存对应的state
## 6). Link: 路由链接
属性1: to="/xxx"
属性2: activeClassName="active"

# 5. react-router的基本使用
## 1). 下载
npm install react-router --save
## 2). 定义各个路由组件
1. About.js
import React from 'react'
function About() {
return <div>About组件内容</div>
}
export default About
2. Home.js
import React from 'react'
function Home() {
return <div>Home组件内容2</div>
}
export default Home
3. Repos.js
import React, {Component} from 'react'
export default class Repos extends Component {
render() {
return (
<div>Repos组件</div>
)
}
}
4. App.js
import React, {Component} from 'react'
import {Link} from 'react-router'

export default class App extends Component {
render() {
return (
<div>
<h2>Hello, React Router!</h2>
<ul>
<li><Link to="/about" activeClassName="active">About2</Link></li>
<li><Link to="/repos" activeClassName="active">Repos2</Link></li>
</ul>
{this.props.children}
</div>
)
}
}
## 3). index.js: 注册路由, 渲染路由器标签
import React from 'react'
import {render} from 'react-dom'
import {Router, Route, IndexRoute, hashHistory} from 'react-router'
import App from './modules/App'
import About from './modules/About'
import Repos from './modules/Repos'
import Home from './modules/Home'

render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="/about" component={About}></Route>
<Route path="/repos" component={Repos}></Route>
</Route>
</Router>
), document.getElementById('app'))

## 3). 主页面: index.html
<style>
.active {
color: red;
}
</style>

# 6. 向路由组件传递请求参数
## 1). repo.js: repos组件下的分路由组件
import React from 'react'
export default function ({params}) {
let {username, repoName} = params
return (
<div>用户名:{username}, 仓库名:{repoName}</div>
)
}
## 2). repos.js
import React from 'react'
import NavLink from './NavLink'

export default class Repos extends React.Component {

constructor(props) {
super(props);
this.state = {
repos: [
{username: 'faceback', repoName: 'react'},
{username: 'faceback', repoName: 'react-router'},
{username: 'Angular', repoName: 'angular'},
{username: 'Angular', repoName: 'angular-cli'}
]
};
this.handleSubmit = this.handleSubmit.bind(this)
}

handleSubmit () {

const repos = this.state.repos
repos.push({
username: this.refs.username.value,
repoName: this.refs.repoName.value
})
this.setState({repos})
this.refs.username.value = ''
this.refs.repoName.value = ''
}

render() {
return (
<div>
<h2>Repos</h2>
<ul>
{
this.state.repos.map((repo, index) => {
const to = `/repos/${repo.username}/${repo.repoName}`
return (
<li key={index}>
<Link to={to} activeClassName='active'>{repo.repoName}</Link>
</li>
)
})
}
<li>
<form onSubmit={this.handleSubmit}>
<input type="text" placeholder="用户名" ref='username'/> / {' '}
<input type="text" placeholder="仓库名" ref='repoName'/>{' '}
<button type="submit">添加</button>
</form>
</li>
</ul>
{this.props.children}
</div>
);
}
}
## 3). index.js: 配置路由
<Route path="/repos" component={Repos}>
<Route path="/repos/:username/:repoName" component={Repo}/>
</Route>

# 7. 优化Link组件
## 1). NavLink.js
import React from 'react'
import {Link} from 'react-router'
export default function NavLink(props) {
return <Link {...props} activeClassName="active"/>
}
## 2). Repos.js
<NavLink to={to}>{repo.repoName}</NavLink>

react-router 基本使用的更多相关文章

  1. [Redux] Filtering Redux State with React Router Params

    We will learn how adding React Router shifts the balance of responsibilities, and how the components ...

  2. [转] React Router 使用教程

    PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...

  3. [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 ...

  4. [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 ...

  5. React Router基础使用

    React是个技术栈,单单使用React很难构建复杂的Web应用程序,很多情况下我们需要引入其他相关的技术 React Router是React的路由库,保持相关页面部件与URL间的同步 下面就来简单 ...

  6. 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

    请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...

  7. react router 4.0以上的路由应用

    thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...

  8. React Router 使用教程

    一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...

  9. 关于react router 4 的小实践

    详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...

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

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

随机推荐

  1. 代码生成codegen

    代码生成codegen 该模块提供了从SymPy表达式生成直接可编译代码的功能.该codegen功能是SymPy中代码生成功能的用户界面.下面为可能希望直接使用框架的高级用户提供了一些实现细节. 注意 ...

  2. Mybatis映射文件中的参数传递

    一.接口中只有一个参数 1.参数是基本类型or基本类型的包装类or字符串类型 这种情况下映射文件中#{}里的内容可以是任意的,你可以使用#{xxx} 或 #{abc} .....因为此时#{}相当于一 ...

  3. mybatis之Param注解

    一.作用 使用@Param注解表示给参数命名,名称就是括号中的内容.给参数命名,然后在映射文件中就能根据名称获取参数值了.在mybatis中我们常常要使用到多个参数,但是在xml中的parameter ...

  4. 三、DNS子域授权

    前提:准备两台虚拟机:父域: www.tedu.cn(虚拟机A)子域:www.bj.tedu.cn(虚拟机B) 一.在两台虚拟机上安装域名解析软件 root@pc207 ~]# yum -y inst ...

  5. 【题解】Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo

    Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo 题目描述 Farmer John and Bessie the cow love to exchange ma ...

  6. IDEA拷贝类路径

    1.方法一 1.1.鼠标右击需要复制的类 1.2.点击 Copy Reference 2.方法二 快捷键:Ctrl + Alt + Shift + C

  7. String ,StringBuffer 与S tringBuilder的区别??

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) ------------------------------------- ...

  8. 深度解读MRS IoTDB时序数据库的整体架构设计与实现

    [本期推荐]华为云社区6月刊来了,新鲜出炉的Top10技术干货.重磅技术专题分享:还有毕业季闯关大挑战,华为云专家带你做好职业规划. 摘要:本文将会系统地为大家介绍MRS IoTDB的来龙去脉和功能特 ...

  9. 《手把手教你》系列基础篇之(三)-java+ selenium自动化测试- 启动三大浏览器(上)(详细教程)

    1.简介 前边宏哥已经将环境搭建好了,今天就在Java项目搭建环境中简单地实践一下: 启动三大浏览器.按市场份额来说,全球前三大浏览器是:IE.Firefox.Chrome.因此宏哥这里主要介绍一下如 ...

  10. SpringCloud Alibaba实战(10:分布式配置中心)

    源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在我们前面介绍Nacos的时候,说到,Nacos除了可以作为注册中心,还可以作为配置 ...