React@16.x(50)路由v5.x(15)源码(7)- 实现 Link 和 NavLink

目录

作用 :最终渲染为 <a> 标签,点击跳转对应路由时不刷新页面。

1.1,注意点

1,Link.to 属性,除了传递 string 之外,还可以传递 location 对象。

2,最终渲染的路由链接中,是包括 basename 的。

1.2,实现

js 复制代码
import React from "react";
import ctx from "../react-router/RouterContext";
import { parsePath } from "history";

export function Link(props) {
    const { to, ...rest } = props;
    return (
        <ctx.Consumer>
            {(value) => {
                let _location = {};
                if (typeof to === "object") {
                    _location = to;
                } else {
                    // 将路径字符串,转为 location 对象。
                    _location = parsePath(to);
                }
                // 通过 location 对象创建一个可用于 a 标签的链接(会自动拼接 basename)。
                const _href = value.history.createHref(_location);
                return (
                    <a
                        href={_href}
                        {...rest}
                        onClick={(e) => {
                            e.preventDefault();
                            value.history.push(_location);
                        }}
                    >
                        {props.children}
                    </a>
                );
            }}
        </ctx.Consumer>
    );
}

2.1,注意点

相比较 Link 组件,NavLink 组件会在路由匹配时,对生成的 <a> 标签添加 active 类名(类名)。

匹配规则受 toexactstrictsensitive 等属性影响。

2.2,实现

js 复制代码
import React from "react";
import { parsePath } from "history";
import { Link } from "./Link";
import ctx from "../react-router/RouterContext";
import matchPath from "../react-router/matchPath";

export function NavLink(props) {
    const { activeClass = "active", exact = false, strict = false, sensitive = false, ...rest } = props;
    return (
        <ctx.Consumer>
            {({ location }) => {
                let _location = {};
                if (typeof props.to === "object") {
                    _location = props.to;
                } else {
                    // 将路径字符串,转为 location 对象。
                    _location = parsePath(props.to);
                }
                const matched = matchPath(_location.pathname, location.pathname, { exact, strict, sensitive });
                if (matched) {
                    return <Link className={activeClass} {...rest}></Link>;
                } else {
                    return <Link {...rest}></Link>;
                }
            }}
        </ctx.Consumer>
    );
}

以上。

相关推荐
JustHappy4 小时前
古法编程秘籍(七):互联网到底是什么?把两台电脑怎么说话搞懂就够了
前端·后端·网络协议
老毛肚4 小时前
jeecg-boot-base-core 02 day
javascript·python
snow@li4 小时前
SEO-文章标题:写文章时候,分类+主标题+大纲+解释 作为标题 / 不点进去也知道全文覆盖什么 / 标题即架构
前端
kyriewen5 小时前
Git Commit 前自动修复代码风格?配置 Husky + lint-staged,从此 CR 只聊逻辑
前端·git·面试
小和尚同志5 小时前
AI 自动化测试探索(一):Playwright MCP
前端·人工智能·aigc
老马识途2.05 小时前
在AI的帮助下理解spring的启动过程
java·前端·spring
徐小夕6 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
运筹vivo@6 小时前
Python ContextVar 底层机制与内存模型拆解
前端·数据库·python
#麻辣小龙虾#7 小时前
基于vue3.0开发一款【固废与废气运维管理系统】(支持源码)
前端·vue.js·vue3
Cosolar8 小时前
Docsify零构建文档站完全指南:从快速搭建到企业级部署
前端·开源·github