1、安装使用

$ npm install -S react-router
import { Router, Route, hashHistory } from 'react-router';

render((
<Router history={hashHistory}>
<Route path="/" component={App}/>
</Router>
), document.getElementById('app'));

1.1、版本问题

react-router 有多个版本,2.x/3.x  - 4.x版本有比较大的改动,并且互相不兼容,2.x/3.x 和 4.x 版本的语法有非常大的不同。并且 react-router 和 react 的某些版本也会有冲突

目前使用 react-router 的 3.x 版本下面的版本可以运行成功。

"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^3.0.2",
"react-scripts": "3.1.1"
}

2、Router 组件

2.1、JSX 的 Route 组件实现路由

路由器Router就是React的一个组件。Router组件本身只是一个容器,真正的路由要通过Route组件定义。

//入口文件 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, hashHistory } from 'react-router'; ReactDOM.render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/repos" component={Repos}/>
</Route>
</Router>
), document.getElementById('app'));

2.1、route 数组对象实现路由

子路由也可以不写在Router组件里面,单独传入Router组件的routes属性。

let routes = <Route path="/" component={App}>
<Route path="/repos" component={Repos}/>
</Route>; <Router routes={routes} history={browserHistory}/>

或者是更方便的写法(推荐写法):

const routes = [{
path: '/',
component: App,
indexRoute: { component: DefaultComponent },
childRoutes: [
{ path: 'about', component: About },
{ path: 'inbox', component: Inbox },
]
}] React.render(<Router routes={routes} />, document.body)

3、路径匹配的组件

react-router 是按 URL 来匹配组件的。

React.render((
<Router>
<Route path="/" component={App}>
<Route path="about" component={About} />
<Route path="inbox" component={Inbox}>
<Route path="messages/:id" component={Message} />
</Route>
</Route>
</Router>
), document.body)

如果 URL 匹配到某个组件,并且 URL 上并没有该组件的上层组件(即包含着它的上层 Route),此时仍然会匹配到该组件的上层 Route。

比如下面:用绝对路径表示 Message 组件,当 URL 是 /message/aaa 时,仍会匹配到 APP 和 Inbox 组件,然后再是 Message 组件。

React.render((
<Router>
<Route path="/" component={App}>
<IndexRoute component={Dashboard} />
<Route path="about" component={About} />
<Route path="inbox" component={Inbox}>
{/* 使用 /messages/:id 替换 messages/:id */}
<Route path="/messages/:id" component={Message} />
</Route>
</Route>
</Router>
), document.body)

想要在某组件内渲染它的下层即子路由,在组件内使用 {this.props.children} 语法。

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. python里一个class可以定义多个构造函数

    不行,一个class只能有一个用于构造对象的__init__函数但python中的变量是无类型的,因此传给__init__的参数可以是任何类型python中的函数参数在定义时可以有默认值,可以让__i ...

  2. Java ——多线程编程

    本节重点思维导图 多线程编程

  3. 根据对象属性查找对象或者数组(根据对象属性查找某数组内符合该条件的对象,数组内对象属性check为true的对象,存放到数组内) 滚动轴样式

      1.根据对象属性查找某数组内符合该条件的对象. optionComwords:[ {optionName:"名称1", optionCode: '1'}, {optionNam ...

  4. mysql数据库负载均衡高可用之主从、主主备份,实时同步

    一:MySQL Replication 什么是MySQL Replication Replication可以实现将数据从一台数据库服务器(master)复制到一或多台数据库服务器(slave) 默认情 ...

  5. 使用 PC 做 FTP/TFTP 服务器,上传和下载文件

    使用PC做TFTP服务器,上传和下载文件需要用到一个工具软件,IPOP,可百度下载. 1.在桌面新建一个空闲的文件夹,作为TFTP服务器的存储位置,然后打开IPOP软件,开启服务. 图片中 编号3 的 ...

  6. php配置伪静态如何将.htaccess文件转换 nginx伪静态文件

    php通常设置伪静态三种情况,.htaccess文件,nginx伪静态文件,Web.Config文件得形式,如何将三种伪静态应用到项目中呢, 1,.htaccess文件 实例 <IfModule ...

  7. Android数据库使用指南(上)

    前言Android上的数据库是sqlite,虽然这个数据库是轻量级的,但是储存的东西可不少,sqlite官方表示理论存储容量为140TB,目前应该没有那么大容量的手机,存储能力太强了. 关于如何使用S ...

  8. 什么是CPC,CPA,CVR,CTR,ROI

    合格的网络营销人员都应该熟悉下面的常见英文缩写,这些都是我们必须知道的名词解释:CVR (Click Value Rate): 转化率,衡量CPA广告效果的指标CTR (Click Through R ...

  9. Ubuntu配置python操作

    Ubuntu16.04 安装python 查看当前python情况root@localhost:/# cd /root@localhost:/usr/bin# cd /usr/binroot@loca ...

  10. TCP软件环境测试

    利用合宙官网上的云平台->TCP透传云,建立一个TCP服务. http://tcplab.openluat.com/ [注意事项] 如3分钟内没有客户端接入则会自动关闭. 每个服务器最大客户端连 ...