手写React Router

栏目: 服务器 · 发布时间: 6年前

内容简介:目前常见的路由分为两种,history是h5新出的api,让我们可以在改变url而不刷新页面,主要的几个api是:

目前常见的路由分为两种, HashRouterBrowserRouter

HashRouter

hash 路由的方式主要是通过改变 hash 值来改变 url ,因为只是改变 hash ,所以并不会触发页面刷新,很适合用在单页应用局部更新, window 提供监听 hashChange 方法来监听hash值的改变去更新我们的单页

window.addEventListener('hashchange', function(e) {
  console.log('hashchange', e)
  // 更新页面
})
复制代码

BrowserRouter

history是h5新出的api,让我们可以在改变url而不刷新页面,主要的几个api是:

  1. History.back() : 返回浏览器会话历史中的上一页,跟浏览器的回退按钮功能相同
  2. History.forward() :指向浏览器会话历史中的下一页,跟浏览器的前进按钮相同。
  3. History.go() : 可以跳转到浏览器会话历史中的指定的某一个记录页。
  4. History.pushState() :pushState可以将给定的数据压入到浏览器会话历史栈中,该方法接收3个参数,对象,title和一串url。pushState后会改变当前页面url,但是不会伴随着刷新。
  5. History.replaceState() :replaceState将当前的会话页面的url替换成指定的数据,replaceState后也会改变当前页面的url,但是也不会刷新页面。 以上只是实现改变 url 不刷新页面,我们还需要监听用户前进后退的变化。 popstate 是history的一个监听事件,可以监听history的变化 好了,基本上就是这个原理

实现

实现部分主要是利用 reactcontext api 来存储路由信息,子组件根据 context 值去渲染,代码是由 hook 实现,不了解的,看之前,学习一下React Hook,下面我们来实现一下 BrowserRouter

BrowserRouter

import React, { useState } from "react";
  
let set; // 保存setUrl,因为监听事件咱们值加入一次,所以放外面
function popstate(e) {
  set(window.location.pathname);
}
// 创建context
export const RouterContext = React.createContext(window.location.pathname);

export default function({ children }) {
  const [url, setUrl] = useState(window.location.pathname);
  set = setUrl;

  window.addEventListener("popstate", popstate);

  const router = {
    history: {
      push: function(url, state, title) {
        window.history.pushState(state, title, url);
        setUrl(url);
      },
      replace: function(url, state, title) {
        window.history.replaceState(state, title, url);
        setUrl(url);
      },
      // 下面也需要嵌入setUrl,暂不处理
      go: window.history.go,
      goBack: window.history.back,
      goForward: window.history.forward,
      length: window.history.length
    },
    url: url
  };

  return (
    <RouterContext.Provider value={router}>{children}</RouterContext.Provider>
  );
}
复制代码

Route

import React, { useContext } from "react";
import { RouterContext } from "./BrowserRouter";

function Route({ component, path }) {
  // 获取context
  const { history, url } = useContext(RouterContext);
  const match = {
    path,
    url
  };
  const Component = component;
  return url === path && <Component history={history} match={match} />;
}

export default Route;
复制代码

Link

import React, { useContext } from "react";
import { RouterContext } from "./BrowserRouter";
import styled from "styled-components";

const A = styled.a`
  text-decoration: none;
  padding: 5px;
`;

function Link({ children, to }) {
  const { history } = useContext(RouterContext);
  const onClick = e => {
    e.preventDefault();
    history.push(to);
  };
  return (
    <A href={to} onClick={onClick}>
      {children}
    </A>
  );
}

export default Link;
复制代码

总结

查看Github源码 其实很简单的一个东西,了解原理很容易实现,看了这篇文章,你也可以试着去实现一下 HashRouter .


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

RabbitMQ实战

RabbitMQ实战

Alvaro Videla、Jason J. W. Williams / 汪佳南 / 电子工业出版社 / 2015-10 / 75.00元

本书对RabbitMQ做了全面、翔实的讲解,体现了两位专家的真知灼见。本书首先介绍了有关MQ的历史,然后从基本的消息通信原理讲起,带领读者一路探索RabbitMQ的消息通信世界。这当中不仅包含了针对单台RabbitMQ服务器和RabbitMQ集群的讲解,还教导读者如何使用各种工具来进行监控。 本书内容浅显易懂,文笔风趣幽默。书中包含了丰富的、可以运行的示例程序源代码,读者可以自行下载并运行,......一起来看看 《RabbitMQ实战》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具