React@16.x(48)路由v5.x(13)源码(5)- 实现 Switch

1,原生 Switch 的渲染内容

对如下代码来说:

import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
function News() {
    return <div className="page news">News</div>;
}

function Goods() {
    return <div className="page goods">Goods</div>;
}

export default function App() {
    return (
        <Router>
            <Switch>
                <Route path="/page1" component={News}></Route>
                <Route path="/page2" component={Goods}></Route>
            </Switch>
        </Router>
    );
}

React 插件展示的内容:

在这里插入图片描述

可以看到,除了也使用了 Router 的上下文之外,只加载了一个 Route 组件

2,实现

经测试,Switch 的子元素有如下规则:

  • 如果不是 Route 组件,则会报错。
  • 如果只有一个 Route 组件,则得到的 props.children 的类型是对象
  • 如果有多个 Route 组件,则得到的 props.children 的类型是数组。

所以,除了做以上特殊的判断外,再加上渲染第一个匹配到的组件的逻辑即可。

import React, { Component } from "react";
import ctx from "./RouterContext";
import { Route } from "./Route";
import matchPath from "./matchPath";

export class Switch extends Component {
    getChildren = ({ location }) => {
        let children = [];
        if (Array.isArray(this.props.children)) {
            children = this.props.children;
        } else if (typeof this.props.children === "object") {
            children = [this.props.children];
        }
        for (const child of children) {
            if (child.type !== Route) {
                throw new TypeError("子元素非 Route 组件");
            }
            const { path = "/", exact = false, strict = false, sensitive = false } = child.props;
            const result = matchPath(path, location.pathname, {
                exact,
                strict,
                sensitive,
            });
            if (result) {
                return child;
            }
        }
        return null;
    };
    render() {
        return <ctx.Consumer>{this.getChildren}</ctx.Consumer>;
    }
}

注意到,判断是否是 Route 组件,可通过引入的 Route 组件直接进行判断。

在这里插入图片描述


以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下雪天的夏风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值