如何理解react

您所在的位置:网站首页 react路由原理解析 如何理解react

如何理解react

2024-07-05 16:47:57| 来源: 网络整理| 查看: 265

如何理解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.页面跳转实现

BrowserHistory:pushState、replaceState;

HashHistory:location.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.浏览器回退

BrowserHistory:popstate;

HashHistory:hashchang;

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.org/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); }); } componentDidMount() { this.route(history.location); this.initListener(); } render() { const { Page } = this.state; return Page && ; } }

目前react-router在项目中已有大量实践,其优点可以总结如下:

风格: 与React融为一体,专为react量身打造,编码风格与react保持一致,例如路由的配置可以通过component来实现

简单: 不需要手工维护路由state,使代码变得简单

强大: 强大的路由管理机制,体现在如下方面

路由配置: 可以通过组件、配置对象来进行路由的配置

路由切换: 可以通过 Redirect进行路由的切换

路由加载: 可以同步记载,也可以异步加载,这样就可以实现按需加载

使用方式: 不仅可以在浏览器端的使用,而且可以在服务器端的使用

当然react-router的缺点就是API不太稳定,在升级版本的时候需要进行代码变动。

更多React相关技术文章,请访问React答疑栏目进行学习!

以上就是如何理解react-router的详细内容,更多请关注html中文网其它相关文章!



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭