react-router 的 useBlocker 路由拦截

开始之前

路由拦截功能是很常见的,功能。比如:我们的表单没有填写完毕,用户就就要跳转,此时我们需要拦截与确认。在 React-router v6 之前提供了 Prompt 组件用于拦截跳转。React Router v6.19.0 版本之后,开始移除 unstable_ 不稳定的前缀变的正式可用

remix 中 useBlocker 从 v2.3.0 开始

Remix 中 useBlocker 钩子函数,从 v2.3.0 开始能够正式使用。

useBlocker 的用法

useBlocker 是一个钩子函数,此钩子函数需要接受一个函数作为参数(或者是 boolean 的值),以下是 useBlocker 相关的 TS 类型:

ts 复制代码
export declare function useBlocker(shouldBlock: boolean | BlockerFunction): Blocker;

export type BlockerFunction = (args: {
    currentLocation: Location;
    nextLocation: Location;
    historyAction: HistoryAction;
}) => boolean;

export type Blocker = BlockerUnblocked | BlockerBlocked | BlockerProceeding;

interface BlockerBlocked {
    state: "blocked";
    reset(): void;
    proceed(): void;
    location: Location;
}
interface BlockerUnblocked {
    state: "unblocked";
    reset: undefined;
    proceed: undefined;
    location: undefined;
}
interface BlockerProceeding {
    state: "proceeding";
    reset: undefined;
    proceed: undefined;
    location: Location;
}

以上是 useBlocker 相关的类型,这些类型

特点

  • useBlocker 拦截的是通过 react-router 导航
  • 如果手动点击浏览器的导航按钮,useBlocker 是不能拦截的

Remix 中使用示例

tsx 复制代码
import React from "react";
import { Button, Form, Modal } from "antd";
import { Link, useBlocker } from "@remix-run/react";


export default function ImportantForm() {
  const [value, setValue] = React.useState("");

  const blocker = useBlocker(
    ({ currentLocation, nextLocation }) =>
      value !== "" && currentLocation.pathname !== nextLocation.pathname
  );

  return (
    <Form method="post">
      <label>
        请输出数据
        <input
          name="data"
          value={value}
          onChange={(e) => setValue(e.target.value)}
        />
      </label>
      <button type="submit">Save</button>
      <br />
      <Button>
        <Link to="/">home</Link>
      </Button>

      {blocker.state === "blocked" ? (
        <div>
          <Modal
            title="Are you sure you want to leave?"
            open={blocker.state === "blocked"}
            onOk={() => blocker.proceed()}
            onCancel={() => blocker.reset()}
          >
            <p>Some contents...</p>
            <p>Some contents...</p>
            <p>Some contents...</p>
          </Modal>
        </div>
      ) : null}
    </Form>
  );
}

小结

本文主要介绍 React Router 的 useBlocker 的用法,以及基本使用案例。useBlocker 在 v6.19.0 开始正式使用,相比对路由拦截有需求的朋友这个 api 一定非常受用。

相关推荐
喵个咪5 分钟前
基于 Next.js 的 Headless CMS 前端架构:技术解析与二次开发导引
前端·react.js·next.js
阿白同学1054517 分钟前
一座前端文明的地层:React 源码考古报告
前端
七牛云行业应用7 分钟前
别手搓多Agent了!Codex Windows版用Git Worktree并行跑代码,真的香
前端
前端环境观察室8 分钟前
指纹浏览器都用了,为什么任务还是要人盯着?
前端
回家路上绕了弯8 分钟前
LangChain4j 万字实战:Java生态最火大模型框架,从入门到企业级RAG与Agent落地
后端
东风微鸣12 分钟前
Rook-Ceph v1.20.0 CSI ServiceAccount 命名不匹配 Bug 及修复方案
后端
lichenyang45315 分钟前
鸿蒙聊天 Demo 练习 11:路由拦截器 + dialog 路由 + 页面生命周期
前端
铁皮饭盒17 分钟前
Bun 提供了许多 Node.js 原生没有的专属 API
前端·后端
destinying19 分钟前
前端秒变AI全栈,我的核心资产是一套Node.js“中间件”
前端·后端·面试
环信27 分钟前
即时通讯服务的数据安全与合规实践
前端