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

react router

1、Router组件

1.1 属性

children结点

history

必传对象

staticContext

对象

router作为context的提供者。其有两层context

render() { return ( <RouterContext.Provider value={{ history: this.props.history, location: this.state.location, match: Router.computeRootMatch(this.state.location.pathname), staticContext: this.props.staticContext }} > <HistoryContext.Provider children={this.props.children || null} value={this.props.history} /> </RouterContext.Provider> ); } }

BrowserRouter和HashRouter基本骨架是一样的,除了history属性,BrowserRouter的history是通过createBrowserHistory创建,而HashRouter的history是通过createHashHistory创建

路由提供者的对象成员有

  • history,其也会作为history的提供值

    action: Action location: Location createHref(to: To): string push(to: To, state?: any): void replace(to: To, state?: any): void go(delta: number): void back(): void forward(): void isten(listener: Listener): () => void block(blocker: Blocker): () => void
  • location

  • match

  • staticContext

1.2 路由对象

StaticRouter:用在服务端渲染

其属性有

属性名说明
basename路由的前缀名

context

上下文信息

location

url信息

2、Route组件

2.1 属性

children函数或者结点
exact布尔类型
location对象
path字符串或者字符串数组
render函数
sensitive布尔值
strict布尔值
component组件

children和component同时配置时,component会被忽略

children和render同时配置时,render会被忽略

route组件中基于router的context以及route组件的属性来构造给路由对应组件的消费的属性,其中包含context,location,match。其中match计算返回的一个包含以下属性的对象

参数说明

path

路径

url

url的匹配部分

isExact

是否精确匹配

params

模板参数
const match = this.props.computedMatch ? this.props.computedMatch // <Switch> already computed the match for us : this.props.path ? matchPath(location.pathname, this.props) : context.match; function matchPath(pathname, options = {}) { if (typeof options === "string" || Array.isArray(options)) { options = { path: options }; } const { path, exact = false, strict = false, sensitive = false } = options; const paths = [].concat(path); return paths.reduce((matched, path) => { if (!path && path !== "") return null; if (matched) return matched; const { regexp, keys } = compilePath(path, { end: exact, strict, sensitive }); const match = regexp.exec(pathname); if (!match) return null; const [url, ...values] = match; const isExact = pathname === url; if (exact && !isExact) return null; return { path, // the path used to match url: path === "/" && url === "" ? "/" : url, // the matched portion of the URL isExact, // whether or not we matched exactly params: keys.reduce((memo, key, index) => { memo[key.name] = values[index]; return memo; }, {}) }; }, null); }

其计算匹配依赖path-to-regexp库

2.2 element和component区别

element是react router v6的写法

component是react router v5以及之前的写法

<Route path="/" component={Home} /> //v5 <Route path="/" element={<Home />} /> //v6

3、Switch组件

作为Router的context的消费者,在子结点中找到匹配路径的route返回

class Switch extends React.Component { render() { return ( <RouterContext.Consumer> {context => { invariant(context, "You should not use <Switch> outside a <Router>"); const location = this.props.location || context.location; let element, match; // We use React.Children.forEach instead of React.Children.toArray().find() // here because toArray adds keys to all child elements and we do not want // to trigger an unmount/remount for two <Route>s that render the same // component at different URLs. React.Children.forEach(this.props.children, child => { if (match == null && React.isValidElement(child)) { element = child; const path = child.props.path || child.props.from; match = path ? matchPath(location.pathname, { ...child.props, path }) : context.match; } }); return match ? React.cloneElement(element, { location, computedMatch: match }) : null; }} </RouterContext.Consumer> ); } }
http://www.jsqmd.com/news/1360644/

相关文章:

  • 2026年度光斑分析仪从紫外吸收检测看:源头实力工厂
  • 江苏装修异味和甲醛是一回事吗?室内空气问题如何正确判断 - 康一科技
  • 原来9.9元是包月计算,那还是很便宜啊
  • 2026义乌卫生间防水靠谱、经验丰富、信誉好的公司推荐:专业厨卫防水,居家干爽舒心(8月防水最新资讯) - 吉林同城获客
  • 英雄联盟Akari助手:你的智能游戏伙伴,让每局对战都更轻松
  • 3分钟实现Mac微信防撤回:本地化解决方案终极指南
  • pdf拆分工具免费盘点:在线本地无水印方案,从WPS到PDF24全测了 - 免费软件工具方法教程
  • 2026常德卫生间防水靠谱、经验丰富、信誉好的公司推荐:专业卫生间防水,幸福满屋(8月防水最新资讯) - 吉林同城获客
  • Unity编辑器工具开发:实现全局图片资源引用分析器
  • 2026SSCI问卷调研辅导,社科问卷数据精准分析 - 艾德思Editsprings
  • Modbus Slave 的使用
  • Godot 4魂系游戏模板:模块化架构与核心系统实现指南
  • 2026 宝藏免费 AI 修图网站|ImageGood,普通人也能出专业大片 - GrowthUME
  • Unity游戏模组开发终极指南:BepInEx框架原理与实战
  • 2026 还在找好用工具?AI 图片编辑推荐 ImageGood,网页直接出片 - GrowthUME
  • 2026中越跨境物流服务白皮书:宁波凯盈诺越南专线全链路一站式供应链解决方案 - GrowthUME
  • 人脑与大模型统一建模于高维认知流形的理论底层机制研究
  • 2026六安高三落榜生别焦虑!共达复读班保底录取,文化课达82分直升本校大专 - 最新资讯
  • 从SEO到GEO:苏州企业如何让品牌成为AI的“标准答案” - 企业信息资讯
  • IntelliJ IDEA 运行 JMeter 源码:从环境搭建到二次开发实战
  • 5分钟快速上手:GetQzonehistory开源工具帮你永久保存QQ空间青春记忆
  • 2026济南历下区历山街道防水补漏维修指南|全屋渗水排查与正规施工避坑攻略 - 吉林同城获客
  • Unity接入百度云人体分析:从原理到实践,实现实时姿态识别与交互
  • 办公室零食哪家性价比高:【衡身堂】业内典范 - 17328623207
  • 如何快速实现华为健康数据导出:Huawei-TCX-Converter完整指南
  • 微信支付无缝对接:wechat-php-sdk实现网页支付与扫码支付教程
  • 跨省/长途病人转运费用怎么算?透明计价全解析--推荐【正源速达】【电话:18802145136】 - 上善若水2026
  • 基于C/S架构的局域网中国象棋对战系统设计与实现
  • 如何用Python自动化抢票脚本5分钟搞定大麦网演唱会门票
  • 解决UE5/UE4开发GPU崩溃:修改Windows TDR超时设置