当前位置: 首页 > news >正文

React@16.x(46)路由v5.x(11)源码(3)- 实现 Router

目录

  • 1,Router 的结构
  • 2,实现
    • 2.1,react-router
      • 1,matchPath.js
      • 2,Router.js
      • 3,RouterContext.jsx
      • 4,index.jsx
    • 2.2,react-router-dom
      • BrowserRouter.jsx
      • index.jsx

1,Router 的结构

1,react-router 用于注入 history 上下文对象,但 history 对象是别人传递给它的。

2,react-router-dom 使用了 react-router,并传递创建 history 对象。

大致如下:

1,react-router-dom.js

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

2,react-router.js

export default class Router extends Component {ctxValue = { //上下文中对象history: {},location : {},match : {},}render() {return <ctx.Provider value={this.ctxValue}>{this.props.children}</ctx.Provider>}
}

3,其他组件使用:

import { BrowserRouter as Router } from "./react-router-dom";export default function App() {return <Router></Router>
}

2,实现

项目目录

react-router-- index.js-- matchPath.js-- Router.js-- RouterContext.js
react-router-dom-- index.js-- BrowserRouter.js

2.1,react-router

1,matchPath.js

用于获取并组装 match 对象,参考 这篇文章。

2,Router.js

import React, { Component } from "react";
import PropTypes from "prop-types";
import ctx from "./RouterContext";
import matchPath from "./matchPath";export class Router extends Component {static propTypes = {history: PropTypes.object.isRequired,children: PropTypes.node,};state = {location: this.props.history.location, // location 作为状态,是因为切换路由时会发生变化。};componentDidMount() {this.unListen = this.props.history.listen((nextLocation, action) => {this.props.history.action = action;this.setState({location: nextLocation,});});}componentWillUnmount() {this.unListen();}ctxValue = {};render() {this.ctxValue.history = this.props.history;this.ctxValue.location = this.state.location;this.ctxValue.match = matchPath("/", this.state.location.pathname);return <ctx.Provider value={this.ctxValue}>{this.props.children}</ctx.Provider>;}
}

3,RouterContext.jsx

提供上下文对象。

import { createContext } from "react";const context = createContext();
context.displayName = "Router"; // 在 React 插件调试时,显示的名字。export default context;

4,index.jsx

export * from "./Router";

2.2,react-router-dom

BrowserRouter.jsx

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

index.jsx

export * from "./BrowserRouter";

接下文实现 Route


以上。

http://www.lryc.cn/news/389591.html

相关文章:

  • openGauss真的比PostgreSQL差了10年?
  • 【国产开源可视化引擎Meta2d.js】快速上手
  • c#与倍福Plc通信
  • 【OceanBase诊断调优】—— 如何通过trace_id找到对应的执行节点IP
  • 鸿蒙开发Ability Kit(程序访问控制):【使用粘贴控件】
  • PL/SQL入门到实践
  • 双非本 985 硕,我马上要入职上海AI实验室大模型算法岗
  • C盘清理和管理
  • 晚上睡觉要不要关路由器?一语中的
  • ardupilot开发 --- 坐标变换 篇
  • git clone 别人项目后正确的修改和同步操作
  • JAVA连接FastGPT实现流式请求SSE效果
  • 二分查找1
  • 什么美业门店管理系统好用?2024美业收银系统软件排名分享
  • 【文件上传】
  • Golang 单引号、双引号和反引号的概念、用法以及区别
  • linux和mysql基础指令
  • JDK 为什么需要配置环境变量
  • ViewBinding的使用(因为kotlin-android-extensions插件的淘汰)
  • IOS Swift 从入门到精通:ios 连接数据库 安装 Firebase 和 Firestore
  • QT4-QT5(6)-const char* QString 乱码转换
  • 报错:RuntimeError_ cuDNN error_ CUDNN_STATUS_EXECUTION_FAILED
  • 黑马点评项目总结1-使用Session发送验证码和登录login和 使用Redis存储验证码和Redis的token登录
  • 【大模型】Vllm基础学习
  • 使用vue动态给同一个a标签添加内容 并给a标签设置hover,悬浮文字变色,结果鼠标悬浮有的字上面不变色
  • 【ajax实战06】进行文章发布
  • Codeforces Round 954 (Div. 3)(A~E)
  • 基于Java微信小程序同城家政服务系统设计和实现(源码+LW+调试文档+讲解等)
  • [21] Opencv_CUDA应用之使用Haar级联的对象检测
  • CXL:拯救NVMe SSD缓存不足设计难题-2