深入理解react-router 路由的实现原理
React Router 是一个基于 React 之上的强大路由库,它可以让你向应用中快速地添加视图和数据流,保持页面与 URL 间的同步。本文从两个方便来解析 react-router 实现原理。一介绍 react-router 的依赖库history;二使用 history 库,实现一个简单的 react-router 路由。
history 介绍
history 是一个 JavaScript 库,可让您在 JavaScript 运行的任何地方轻松管理会话历史记录。history 抽象出各种环境中的差异,并提供最小的 API ,使您可以管理历史堆栈,导航,确认导航以及在会话之间保持状态。
history 有三种实现方式
1、BrowserHistory用于支持 HTML5 历史记录 API 的现代 Web 浏览器(请参阅跨浏览器兼容性)
2、HashHistory用于旧版Web浏览器
3、MemoryHistory用作参考实现,也可用于非 DOM 环境,如 React Native 或测试
三种实现方法,都是创建了一个 history 对象,这里主要讲下前面两种
const history = { length: globalHistory.length, action: "POP", location: initialLocation, createHref, push, // 改变location replace, go, goBack, goForward, block, listen //监听路由变化 };
1.页面跳转实现
BrowserHistorypushState、replaceState;
HashHistorylocation.hash、location.replace
function push(){ createKey(); // 创建location的key,用于唯一标示该location,是随机生成的 if(BrowserHistory){ globalHistory.pushState({ key, state }, null, href); }else if(HashHistory){ window.location.hash = path; } //上报listener 更新state ... } function replace(){ createKey(); // 创建location的key,用于唯一标示该location,是随机生成的 if(BrowserHistory){ globalHistory.replaceState({ key, state }, null, href); }else if(HashHistory){ window.location.replace(window.location.href.slice(0, hashIndex >= 0 ? hashIndex : 0) + "#" path); } //上报listener 更新state ... }
2.浏览器回退
BrowserHistorypopstate;
HashHistoryhashchang;
if(BrowserHistory){ window.addEventListener("popstate", routerChange); }else if(HashHistory){ window.addEventListener("hashchange", routerChange); } function routerChange(){ const location = getDOMLocation(); //获取location //路由切换 transitionManager.confirmTransitionTo(location,callback=()=>{ //上报listener transitionManager.notifyListeners(); }); }
通过 history 实现简单 react-router
import { Component } from 'react'; import createHistory from 'history/createHashHistory'; const history = createHistory(); //创建 history 对象 / 配置路由表 @type {{"/": string}} / const router = { '/': 'page/home/index', '/my': 'page/my/index' } export default class Router extends Component { state = { page: null } async route(location) { let pathname = location.pathname; let pagePath = router[pathname]; // 加 ./的原因 https://webpack.docschina./api/module-methods#import- const Page = await import(`./${pagePath}`); //获取路由对应的ui //设置ui this.setState({ Page: Page.default }); } initListener(){ //监听路由切换 history.listen((location, action) => { //切换路由后,更新ui this.route(location); }); } ponentDidMount() { this.route(history.location); this.initListener(); } render() { const { Page } = this.state; return Page && <Page {...this.props} />; } }
目前react-router在项目中已有大量实践,其优点可以如下:
风格: 与React融为一体,专为react量身打造,编码风格与react保持一致,例如路由的配置可以通过ponent来实现
简单: 不需要手工维护路由state,使代码变得简单
强大: 强大的路由管理机制,体现在如下方面
- 路由配置: 可以通过组件、配置对象来进行路由的配置
- 路由切换: 可以通过<Link> Redirect进行路由的切换
- 路由加载: 可以同步记载,也可以异步加载,这样就可以实现按需加载
使用方式: 不仅可以在浏览器端的使用,而且可以在服务器端的使用
react-router的缺点就是API不太稳定,在升级版本的时候需要进行代码变动。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指