【10】react 之 react-router
1.1. 路由
路由:URL与处理器的映射。
浏览器当前的 URL 发生变化时,路由系统会做出一些响应,用来保证用户界面与 URL 的同步。
1.2. Router安装
npm i react-router -save
1.3. Router使用
路由器Router就是React的一个组件,所以使用方式跟React组件使用方式一样。
- import {Router ,Route,hashHistory} from 'react-router';
- ReactDOM.render(<Router/>,document.getElementById('app'));
Router组件本身只是一个容器,真正的东西(路由)要通过Route组件定义。
- import React from 'react';
- import ReactDOM from 'react-dom';
- import {Router,Route,hashHistory} from 'react-router';
- import Home from './components/home';
- import About from './components/about';
- ReactDOM.render(
- <Router history={hashHistory}>
- <Route path='/about' component={About}/>
</Router>,- document.getElementById('app')
- );
Router: 路由器对象
history=hashHistoryRouter路由采用hash方式监听地址栏变化,并将URL解析(#后面的内容)为位置对象,路由器可以使用该位置对象(#后面的内容)来匹配路由并呈现正确的组件集。
http://localhost:8000/#/home <Route path='/home' component={Home} />
http://localhost:8000/#/about <Route path='/about' component={About} />
Route :路由对象
path :路径
component 显示的组件
1.4. Router嵌套
一个应用由多个组件构成,复杂的应用中,可能存在嵌套的问题。嵌套路由非常简单,你只需要做两件事情:
1、Route中配置嵌套的Route;
- render((
- <Router history={ hashHistory }>
- <Route path="/" component={ App }>
- <Route path="select" component={ Select }></Route>
- <Route path="found" component={ Found }></Route>
- <Route path="follow" component={ Follow }></Route>
- <Route path="my" component={ My }>
- <Route path=":myname" component={ MyName }></Route>
- <Route path="mysex" component={ MySex }></Route>
- </Route>
- </Route>
- </Router>
- ), document.getElementById('root'));
2、嵌套Route的父组件中使用{this.props.children}渲染子Route的组件即可;
- import React from 'react';
- import ReactDOM from 'react-dom';
- import {Router,Route,hashHistory} from 'react-router';
- import App from './components/App'
- import Home from './components/home';
- import About from './components/about';
- ReactDOM.render(
- <Router history={hashHistory}>
- <Route path='/' component={App} >
- <Route path='home' component={Home}/>
- <Route path='about' component={About}/>
- </Route>
- </Router>,
- document.getElementById('app')
- );
Route / 中嵌套了”/home”和”/about”两个路由。
App.js
- import React,{Component} from 'react';
- class App extends Component{
- render(){
- return (<div>
- <nav>
- <ul>
- <li><a href='#/home'>首页</a></li>
- <li><a href='#/about'>关于我们</a></li></ul>
- </nav>
- <main>{this.props.children} //显示嵌套路由的组件</main>
- </div>
- );
- }
- }
- export default App;
App组件的this.props.children属性就是子组件。
1.4.1. 补充
子路由也可以不写在Router组件里面,单独传入Router组件的routes属性。
- var routes = <Route path="/" component={App}>
- <Route path="home" component={Home}/>
- <Route path="about" component={About}/>
- </Route>;
- <Router routes={routes} history={hashHistory}/>
Route可以无限级的嵌套,只需要遵循响应结构即可,另外不要忘记在嵌套Route的父组件中使用{this.props.children}显示子路由组件。
1.5. path 属性
Route组件的path属性指定路由的匹配规则。如果未提供,路由器将尝试匹配子路由。
注意点:它将与父路由的路径相连,除非它以/开头,使其成为绝对路径。
- <Route path='/' component={App} >
- <Route path='home' component={Home}/>
- <Route path='about' component={About}/>
- </Route>
1.5.1. 路径参数
URL参数:地址中可以传入参数,采用:paramName
- <Route path='users/:userId' component={User}/>
路径中参数都会传入到组件,可以通过this.props.routeParams获取,比如获取userId,this.props.routeParams.userId。
1.6. IndexRoute组件
- <Router history={hashHistory}>
- <Route path='/' component={App} >
- <Route path='home' component={Home}/>
- <Route path='about' component={About}/>
- </Route>
- </Router>
配置了一个嵌套路径,但是访问“/”的时候,你会发现只有App组件本身的内容,只有一个导航菜单,可能我们想默认进入首页的时候就显示Home子组件,那么我们就需要IndexRoute。
IndexRoute:设置默认显示的子组件。
- import {IndexRoute} from 'react-router';
- <Router history={hashHistory}>
- <Route path='/' component={App} >
- <IndexRoute component={Home} /><Route path='home' component={Home}/>
- <Route path='about' component={About}/>
- </Route>
- </Router>
IndexRoute 把{Home}组件设置为默认加载显示的子组件,及路径为“/”,那么Route /home就可以删除了,然后把导航的路径修改为“#/”即可。
- <li><a href='#/home'>首页</a></li>
- <li><a href='#/'>首页</a></li>
1.7. IndexRedirect 组件(了解)
IndexRedirect组件用于访问根路由的时候,将用户重定向到某个子路由。浏览器地址将会改变为”#/home”.
- import {IndexRedirect} from 'react-router';
- <Route path="/" component={App}>
- <IndexRedirect to="home" />
- <Route path="home" component={Home} />
- <Route path="about" component={About} />
- </Route>
上面代码中,用户访问根路径时,将自动重定向到子组件Home。
1.8. Link 链接
Link组件用于取代<a>元素,生成一个链接,允许用户点击后跳转到另一个路由。它基本上就是<a>元素的React 版本,可以接收Router的状态。
- <ul>
- <li><a href='#/'>首页</a></li>
- <li><a href='#/about'>关于我们</a></li>
- </ul>
与上面等价
- import {Link} from 'react-router';
- <ul>
- <li><Link to='/'>首页</Link></li>
- <li><Link to='about'>关于我们</Link></li>
- </ul>
如果希望当前的路由与其他路由有不同样式,这时可以使用Link组件的activeStyle或activeClassName属性。
- <li><Link to='/' activeStyle={{color:'orange'}} onlyActiveOnIndex={true}>首页</Link></li>
- <li><Link to='about' activeStyle={{color:'orange'}}>关于我们</Link></li>
上面代码中,当前页面的链接的style会包含color:’orange’。
- <ul>
- <li><Link to='/' activeClassName='active' onlyActiveOnIndex={true}>首页</Link></li>
- <li><Link to='/about' activeClassName='active'>关于我们</Link></li>
- </ul>
上面代码中,当前页面的链接的class会包含active。
onlyActiveOnIndex :如果为true,当前路径必须与路由路径完全匹配才为活动状态。默认为false,如果子路径为活动状态那么父路径也为活动,因为子路由路径总是包含父路由路径。
除了使用a、link标签之外导航到路由页面,也可以使用props中router对象,该属性是由路由器传入。
- this.props.router.push("/about");
1.9. IndexLink 链接
如果链接到根路由/,不要使用Link组件,而要使用IndexLink组件。
这是因为对于根路由来说,activeStyle和activeClassName会失效,或者说总是生效,因为/会匹配任何子路由。而IndexLink组件会使用路径的精确匹配等效于<Link onlyActiveOnIndex={true}>。
- <IndexLink to="/" activeClassName="active">首页</IndexLink>
1.10. 路由的回调
路由有三个回调函数:
onEnter(nextState, replace, callback?) 进入该路由时调用
onChange(prevState, nextState, replace, callback?) 当路由位置改变时调用,但路由本身不进入或离开。 例如,当路由的子项更改时,或当查询参数更改时,将调用此方法。
onLeave(prevState) 离开该路由时调用
- <Route path='/about' component={About} onEnter={()=>{alert('你来了')}} onLeave={()=>{alert('你走了');}}/>
1.11. 异步路由
路由可以通过采用异步的方式加载组件,从而达到按需加载的目的,大大提供了程序的运行速度。
实现异步路由,提供getComponent(nextState, callback)即可:
- function asyncGetComponent(nextState, cb){
- //异步加载
- require.ensure([], function (require) {
- //回调告诉路由器已经加载完毕
- //null:没有错误
- //require('./components/Async').default
- cb(null, require('./components/Async').default)
- });
- }
- <Route path='async' getComponent={asyncGetComponent} />
1.12. histroy 属性
Router组件的history属性,用来监听浏览器地址栏的变化,并将URL解析成一个地址对象,供 React Router 匹配。
history属性,一共可以设置三种值。
browserHistory
hashHistory(#)
createMemoryHistory
如果设为hashHistory,路由将通过URL的hash部分(#)切换,URL的形式类似localhost/#/about。
- import { hashHistory } from 'react-router'
- render(
- <Router history={hashHistory} routes={routes} />,
- document.getElementById('app')
- )
如果设为browserHistory,浏览器的路由就不再通过hash完成了,而显示正常的路径localhsot/about,背后调用的是浏览器的History API。
- import { browserHistory } from 'react-router'
- render(
- <Router history={browserHistory} routes={routes} />,
- document.getElementById('app')
- )
但是,这种情况需要对服务器改造。否则用户直接向服务器请求某个子路由,会显示网页找不到的404错误。
如果开发服务器使用的是webpack-dev-server,加上--history-api-fallback参数就可以了。
$ webpack-dev-server --inline --content-base . --history-api-fallback
createMemoryHistory主要用于服务器渲染。它创建一个内存中的history对象,不与浏览器URL互动。
const history = createMemoryHistory(location)
【10】react 之 react-router的更多相关文章
- 10分钟学会React Context API
Create-react-app来学习这个功能: 注意下面代码红色的即可,非常简单. 在小项目里Context API完全可以替换掉react-redux. 修改app.js import React ...
- 10 个打造 React.js App 的最佳 UI 框架
10 个打造 React.js App 的最佳 UI 框架 在本文中,我们将分享一些助你打造 React.js App 最佳的 UI 框架.它们具备你所需要的基本 React 组件,以及易用的 API ...
- React Navigation & React Native & React Native Navigation
React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...
- 《React Native 精解与实战》书籍连载「React 与 React Native 简介」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- JavaScript 和 React,React用了大量语法糖,让JS编写更方便。
https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...
- 用react-service做状态管理,适用于react、react native
转载自:https://blog.csdn.net/wo_shi_ma_nong/article/details/100713151 . react-service是一个非常简单的用来在react.r ...
- GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)
GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频) GraphQL + React Apoll ...
- GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)
GraphQL + React Apollo + React Hook 大型项目实战(32 个视频) GraphQL + React Apollo + React Hook 大型项目实战 #1 介绍「 ...
- react实战 : react 与 svg
有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个波浪效果组件,而这个一般用 SVG 做. 所以就变成了在 react 中使用 SVG 的问题. 首先是波浪效果需要的样式. . ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
随机推荐
- C# 理解FileInfo类的Open()方法
我们在对文件进行读写操作之前必须打开文件,FileInfo类为我们提供了一个Open()方法,该方法包含了两个枚举类型值的参数,一个为FileMode枚举类型值,另一个为FileAccess枚举类型值 ...
- 2018年ElasticSearch6.2.2教程ELK搭建日志采集分析系统(目录)
章节一 2018年 ELK课程计划和效果演示 1.课程安排和效果演示 简介:课程介绍和主要知识点说明,ES搜索接口演示,部署的ELK项目演示 章节二 elasticSearch 6.2版本基础讲解到 ...
- HTML <input> 标签如何屏蔽浏览器的自动填写?
autocomplete = "off",实测无效. <input type="text" autocomplete = "off"/ ...
- 简述在php中 = 、==、 === 的区别(简述在php中 等于 、双等于、 三等于 的区别)
= 是赋值:就是说给一个变量赋值 == 是轻量级的比较运算,只看值不看类型 === 是重量级的比较运算,既看值,也看类型,要绝对相等才会为true
- django_数据库操作—增、删、改、查
增加 增加数据有两种方法 1> sava >>> from datetime import date >>> book = BookInfo( btitle= ...
- Jack Straws POJ - 1127 (简单几何计算 + 并查集)
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...
- Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner
D. Robot Vacuum Cleaner time limit per test 1 second memory limit per test 256 megabytes Problem Des ...
- C语言中可变参数的使用
在C语言程序编写中我们使用最多的函数一定包括printf以及很多类似的变形体.这个函数包含在C库函数中,定义为 int printf( const char* format, ...); 除了一个格式 ...
- 51nod 1107 斜率小于零连线数量 特调逆序数
逆序数的神题.... 居然是逆序数 居然用逆序数过的 提示...按照X从小到大排列,之后统计Y的逆序数... 之后,得到的答案就是传说中的解(斜率小于零) #include<bits/stdc+ ...
- Android 简历+面试题 汇总
1.教你写简历 1.1.你真的会写简历吗? 1.2.80%以上简历都是不合格的 1.3.推荐两个技术简历模板 1.4.关于程序员求职简历 1.5.程序员简历模板列表 2.面试题 2.1.国内一线互联网 ...