react 路由的基本原理及实现

news2025/2/24 9:34:19

1. react 路由原理

不同路径渲染不同的组件
有两种实现方式
● HasRouter 利用hash实现路由切换
● BrowserRouter 实现h5 API实现路由切换

1. 1 HasRouter

利用hash 实现路由切换
在这里插入图片描述

1.2 BrowserRouter

利用h5 Api实现路由的切换

1.2.1 history
  • HTML5规范给我们提供了一个history接口
  • HTML5 HIstory API包含两个方法:history.pushState()和history.replaceState(),和一个事件
    window.onpopstate pushState
1.2.1.1 history.pushState(stateObject,title,url)

● 第一个参数用于存储该url对应的状态对象,该对象可在onpopstate事件中获取,也可在history对象中获取
● 第二个参数是标题,目前浏览器并未实现
● 第三个参数是设定的url
pushState函数向浏览器的历史堆栈中压入一个url为设定值的记录,并改变历史堆栈的当前指针至栈顶

1.2.1.2 replaceState

● 该接口与pushState参数相同,含义 也相同
● 唯一的区别在于replaceState是替换浏览器历史栈中的当前历史记录为设定的url
● 需要注意的是replaceState 不会改动浏览器历史堆栈的当前指针

1.2.1.3 onpopstate

● 该事件是window属性
● 该事件会在调用浏览器的前进,后退以及在执行history.forward,history.back 和history.go 的时候触发。因为这些操作有一个共性,即修改了历史堆栈的当前指针
● 在不改变document 的前提下,一旦触发当前指针改变则会触发onpopstate事件

2 实现基本路由

2.1 HashRouter 基本用法及实现

import React from 'react';
import { Router } from '../react-router';
import { createHashHistory } from '../history';
class HashRouter extends React.Component {
    constructor(props) {
        super(props);
        this.history = createHashHistory(props)
    }
    render() {
        return (
            <Router history={this.history}>
                {this.props.children}
            </Router>
        )
    }
}
export default HashRouter;

history 下的 createHashHistory.js

/**
 * 工厂方法,用来返回一个历史对象
 */
function createHashHistory(props) {
    let stack = [];//模拟一个历史条目栈,这里放的都是每一次的location
    let index = -1;//模拟一个当前索引
    let action = 'POP';//动作
    let state;//当前状态
    let listeners = [];//监听函数的数组
    let currentMessage;
    let userConfirm = props.getUserConfirmation?props.getUserConfirmation():window.confirm;
    function go(n) {//go是在历史条目中跳前跳后,条目数不会发生改变
        action = 'POP';
        index += n;
        if(index <0){
            index=0;
        }else if(index >=stack.length){
            index=stack.length-1;
        }
        let nextLocation = stack[index];
        state=nextLocation.state;
        window.location.hash = nextLocation.pathname;//用新的路径名改变当前的hash值
    }
    function goForward() {
        go(1)
    }
    function goBack() {
        go(-1)
    }
    let listener = ()=>{
        let pathname = window.location.hash.slice(1);// /users#/api  /api
        Object.assign(history,{action,location:{pathname,state}}); 
        if(action === 'PUSH'){
          stack[++index]=history.location;//1 2 3 6 5 
          //stack.push(history.location);
        }
        listeners.forEach(listener=>listener(history.location));
     }
    window.addEventListener('hashchange',listener);
    //to={pathname:'',state:{}}
    function push(to,nextState){
        action = 'PUSH';
        let pathname;
        if(typeof to === 'object'){
            state = to.state;
            pathname = to.pathname;
        }else {
            pathname = to;
            state = nextState;
        }
        if(currentMessage){
            let message = currentMessage({pathname});
            let allow = userConfirm(message);
            if(!allow) return;
        }
        window.location.hash = pathname;
    }
    function listen(listener) {
        listeners.push(listener);
        return function () {//取消监听函数,如果调它的放会把此监听函数从数组中删除
            listeners = listeners.filter(l => l !== listener);
        }
    }
    function block(newMessage){
        currentMessage = newMessage;
        return ()=>{
            currentMessage=null;
        }
    }
    const history = {
        action,//对history执行的动作
        push,
        go,
        goBack,
        goForward,
        listen,
        location:{pathname:window.location.hash.slice(1),state:undefined},
        block
    }
    if(window.location.hash){
        action = 'PUSH';
        listener();
    }else{
        window.location.hash='/';
    }
    return history;
}

export default createHashHistory;

2.2 BrowserRouter基本用法及实现

import React from 'react';
import { Router } from '../react-router';
import { createBrowserHistory } from '../history';
class BrowserRouter extends React.Component {
    constructor(props) {
        super(props);
        this.history = createBrowserHistory(props)
    }
    render() {
        return (
            <Router history={this.history}>
                {this.props.children}
            </Router>
        )
    }
}
export default BrowserRouter;

history 下的 createBrowserHistory.js

/**
 * 工厂方法,用来返回一个历史对象
 */
function createBrowserHistory(props){
  let globalHistory = window.history;
  let listeners = [];
  let currentMessage;
  let userConfirm = props.getUserConfirmation?props.getUserConfirmation():window.confirm;
  function go(n){
    globalHistory.go(n);
  }
  function goForward(){
    globalHistory.goForward();
  }
  function goBack(){
    globalHistory.goBack();
  }
  function listen(listener){
    listeners.push(listener);
    return function(){//取消监听函数,如果调它的放会把此监听函数从数组中删除
        listeners = listeners.filter(l=>l!==listener);
    }
  }
  window.addEventListener('popstate',(event)=>{//push入栈 pop类似于出栈
    setState({action:'POP',location:{state:event.state,pathname:window.location.pathname}});
  });
  function setState(newState){
    Object.assign(history,newState);
    history.length = globalHistory.length;
    listeners.forEach(listener=>listener(history.location));
  }
  /**
   * push方法
   * @param {*} path 跳转的路径
   * @param {*} state 跳转的状态
   */
  function push(to,nextState){//对标history pushState
     const action = 'PUSH';
     let pathname;
     let state;
     if(typeof to === 'object'){
         state = to.state;
         pathname = to.pathname;
     }else {
         pathname = to;
         state = nextState;
     }
     if(currentMessage){
      let message = currentMessage({pathname});
      let allow = userConfirm(message);
      if(!allow) return;
     }
     globalHistory.pushState(state,null,pathname);
     let location = {state,pathname};
     setState({action,location});
  }
  function block(newMessage){
    currentMessage = newMessage;
    return ()=>{
        currentMessage=null;
    }
}
  const history = {
    action:'POP',//对history执行的动作
    push,
    go,
    goBack,
    goForward,
    listen,
    location:{pathname:window.location.pathname,state:globalHistory.state},
    block
  }
  return history;
}

export default createBrowserHistory;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1474864.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Go 快速入门】协程 | 通道 | select 多路复用 | sync 包

文章目录 前言协程goroutine 调度使用 goroutine 通道无缓冲通道有缓冲通道单向通道 select 多路复用syncsync.WaitGroupsync.Mutexsync.RWMutexsync.Oncesync.Map 项目代码地址&#xff1a;05-GoroutineChannelSync 前言 Go 1.22 版本于不久前推出&#xff0c;更新的新特性可…

机器视觉运动控制一体机在光伏汇流焊机器人系统的解决方案

一、市场应用背景 汇流焊是光伏太阳能电池板中段加工工艺&#xff0c;其前道工序为串焊&#xff0c;在此环节流程中&#xff0c;需要在多个太阳能电池片表面以平行方式串焊多条焊带&#xff0c;形成电池串。串焊好的多组电池串被有序排列输送到汇流焊接工作台&#xff0c;通过…

MWC 2024丨美格智能推出5G RedCap系列FWA解决方案,开启5G轻量化新天地

2月27日&#xff0c;在MWC 2024世界移动通信大会上&#xff0c;美格智能正式推出5G RedCap系列FWA解决方案。此系列解决方案具有低功耗、低成本等优势&#xff0c;可以显著降低5G应用复杂度&#xff0c;快速实现5G网络接入&#xff0c;提升FWA部署的经济效益。 RedCap技术带来了…

Data Leakage and Evaluation Issues inMicro-Expression Analysis 阅读笔记

IEEE Transactions on Affective Computing上的一篇文章&#xff0c;做微表情识别&#xff0c;阅读完做个笔记。本文讨论了Data Leakage对模型准确度评估的影响&#xff0c;及如何融合多个微表情数据集&#xff0c;从而提升模型的准确度。工作量非常饱满&#xff0c;很认真&…

git忽略某些文件(夹)更改方法

概述 在项目中&#xff0c;常有需要忽略的文件、文件夹提交到代码仓库中&#xff0c;在此做个笔录。 一、在项目根目录内新建文本文件&#xff0c;并重命名为.gitignore&#xff0c;该文件语法如下 # 以#开始的行&#xff0c;被视为注释. # 忽略掉所有文件名是 a.txt的文件.…

基于SSM的校园订餐系统(有报告)。Javaee项目。ssm项目。

演示视频&#xff1a; 基于SSM的校园订餐系统&#xff08;有报告&#xff09;。Javaee项目。ssm项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring Spri…

5G-A,未来已来

目前&#xff0c;全国首个5G-A规模组网示范完成。这项由北京联通携手华为共同打造的示范项目&#xff0c;实现了北京市中心金融街、历史建筑长话大楼、大型综合性体育场北京工人体育场三个重点场景的连片覆盖。 实际路测结果显示&#xff0c;5G-A用户下行峰值速率达到10Gbps&am…

ky10-server docker 离线安装包、离线安装

离线安装脚本 # ---------------离线安装docker------------------- rpm -Uvh --force --nodeps *.rpm# 修改docker拉取源为国内 rm -rf /etc/docker mkdir -p /etc/docker touch /etc/docker/daemon.json cat >/etc/docker/daemon.json<<EOF{"registry-mirro…

弱结构化日志 Flink SQL 怎么写?SLS SPL 来帮忙

作者&#xff1a;潘伟龙&#xff08;豁朗&#xff09; 背景 日志服务 SLS 是云原生观测与分析平台&#xff0c;为 Log、Metric、Trace 等数据提供大规模、低成本、实时的平台化服务&#xff0c;基于日志服务的便捷的数据接入能力&#xff0c;可以将系统日志、业务日志等接入 …

【LeetCode刷题】146. LRU 缓存

请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中&#xff0c;则返回关键字的值&#xff0c;否则返回 -…

MYSQL学习笔记:索引

MYSQL学习笔记&#xff1a;索引 文章目录 MYSQL学习笔记&#xff1a;索引索引的分类索引的创建删除索引优化B树索引B树InnoDB主键和二级索引树聚集索引与非聚集索引哈希索引INNODB的自适应哈希索引索引和慢查询 用索引也是要涉及磁盘I/O的操作的索引也是一种数据结构&#xff0…

【计算机网络】数据链路层|封装成帧|透明传输|差错检测|PPP协议|CSMA/CD协议

目录 一、思维导图 ​ 二、数据链路层功能概述 1.数据链路层概述 2.数据链路层功能概述——封装成帧 3.数据链路层功能概述——透明传输 4.数据链路层功能概述——差错检测 三、数据链路层重要协议 1.数据链路层重要协议&#xff1a;PPP协议 2.数据链路层重要协议&#x…

LeetCode 刷题 [C++] 第141题.环形链表

题目描述 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置&a…

2024数字中国创新大赛·数据要素赛道“能源大数据应用赛”正式上线!参赛指南请查收

近日&#xff0c;由国网福建电力承办的2024数字中国创新大赛能源大数据应用赛正式上线发布。赛事按照数字中国建设、能源革命的战略要求&#xff0c;围绕能源数据要素x、能源数字技术、能源商业模式等热点设置赛题&#xff0c;诚邀社会各界为加快建成新型电力系统出谋划策&…

DVWA 靶场之 Command Injection(命令执行)middlehigh

对于 middle 难度的 我们直接先看源码 <?phpif( isset( $_POST[ Submit ] ) ) {// Get input$target $_REQUEST[ ip ];// Set blacklist$substitutions array(&& > ,; > ,);// Remove any of the characters in the array (blacklist).$target str_rep…

VUE3搭载到服务器

1.搭建服务器 使用 Windows 自带的 IIS 作为服务器。 步骤如下&#xff1a;https://blog.csdn.net/qq_62464995/article/details/130140673 同时&#xff0c;上面的步骤中&#xff0c;还使用了 cpolar 将 IIS 本地网址映射到公共网址。 注&#xff1a; cpolar客户端&#xf…

java springmvc/springboot 项目通过HttpServletRequest对象获取请求体body工具类

请求 测试接口 获取到的 获取到打印出的json字符串里有空格这些&#xff0c;在json解析的时候正常解析为json对象了。 工具类代码 import lombok.extern.slf4j.Slf4j; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.we…

(全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF

研究生英语读写教程基础级教师用书PDF 研究生英语读写教程提高级教师用书PDF pdf下载&#xff08;完整版下载&#xff09; &#xff08;1&#xff09;研究生英语读写教程基础级教师用书PDF &#xff08;2&#xff09;研究生英语读写教程基提高级教师用书PDF

yolov9 瑞芯微芯片rknn部署、地平线芯片Horizon部署、TensorRT部署

特别说明&#xff1a;参考官方开源的yolov9代码、瑞芯微官方文档、地平线的官方文档&#xff0c;如有侵权告知删&#xff0c;谢谢。 模型和完整仿真测试代码&#xff0c;放在github上参考链接 模型和代码。 之前写过yolov8检测、分割、关键点模型的部署的多篇博文&#xff0c;y…

网络安全之内容安全

内容安全 攻击可能只是一个点&#xff0c;防御需要全方面进行 IAE引擎 DFI和DPI技术--- 深度检测技术 DPI --- 深度包检测技术--- 主要针对完整的数据包&#xff08;数据包分片&#xff0c;分段需要重组&#xff09;&#xff0c;之后对 数据包的内容进行识别。&#xff08;应用…