@fast-link/react

中文文档
0.0.2发布于 2026/11/03MITTypeScriptES Module构建来源已验证近 30 天下载 1万

React lifecycle binding for Fast Link DOM controllers

原始使用文档
文档目录6 节
  1. Requirements
  2. Install the package
  3. Bind navigation to a React region
  4. useFastLink reference
  5. Understand the hook lifecycle
  6. Related packages

▲ @fast-link/react

@fast-link/react binds a @fast-link/core controller to a React-owned anchor or navigation region. Use it when an application or framework adapter needs its own routing policy.

For Next.js App Router applications, use @fast-link/next instead.

For press timing and opt-outs, see when to use Fast Link.

Sites requiring WCAG Level A or AA conformance should leave Fast Link disabled. See the accessibility limitation.

Requirements

Supports Node.js 22 or later, with React 19 (>=19 <20).

Install the package

npm install @fast-link/react

The Fast Link agent skill is included at skills/fast-link/SKILL.md.

Bind navigation to a React region

Navigation starts on press, before a click handler can cancel it. Keep links that need confirmation, release-time checks, or expected URL dragging native by adding data-no-fast-link to the anchor.

useFastLink returns ref and click-capture bindings. Spread both on the same anchor or element containing anchors, after its other props:

'use client';

import { useFastLink } from '@fast-link/react';
import type { ReactNode } from 'react';

export function AppRegion({
  children,
  route,
}: {
  children: ReactNode;
  route: (destination: URL) => void;
}) {
  const attach = useFastLink<HTMLDivElement>({
    navigate: ({ url }) => route(url),
  });

  return (
    <div {...attach} style={{ display: 'contents' }}>
      {children}
    </div>
  );
}

Primary mouse navigation inside the region now calls route on pointerdown. The intent provides the source anchor, its authored href, and a resolved same-origin URL. The application handles routing, history, scroll, route state, and transitions.

The package renders no wrapper and imports no router. The display: contents element in the example belongs to the application.

function useFastLink<T extends HTMLElement = HTMLElement>(
  options: FastLinkOptions & {
    forwardedRef?: ForwardedRef<T>;
    onClickCapture?: MouseEventHandler<T>;
  },
): {
  ref: (element: T | null) => void;
  onClickCapture: MouseEventHandler<T>;
};
OptionTypeDefaultBehavior
enabledbooleantrueControls whether new pointerdowns can start navigation
navigate(intent) => voidwindow.location.assign(intent.url.href)Performs navigation after onFastNavigate; its return value is ignored
onFastNavigate(intent) => voidNoneRuns synchronously before navigate
onClickCaptureMouseEventHandler<T>NoneRuns after core suppression, including for prevented clicks
forwardedRefForwardedRef<T>nullComposes the caller's ref with the controller ref

Core's onFastNavigate observes the accepted intent before navigate; it cannot cancel navigation through a return value or preventDefault(). If it throws, navigate still runs, then the error surfaces. A navigation error takes precedence if both callbacks throw. The follow-up click remains suppressed.

The returned object, ref, and capture handler are stable. Pass a caller capture handler through onClickCapture; the hook runs it after suppression, even when the click is prevented. Spreading the bindings last preserves that composition. A ref alone cannot suppress a click stopped by descendant React capture.

Place regions inside the DOM subtree they control, including portal and shadow trees. React ancestry alone does not extend a region's DOM scope.

Understand the hook lifecycle

React manages the controller attached to the element receiving the returned ref:

Attach root
  -> create controller

Commit options
  -> replace every copied option
  -> keep pending click

Remove root or unmount
  -> destroy controller

Replace root
  -> destroy old controller
  -> create new controller
  • Destroying the controller clears pending click state and removes listeners.
  • Committed caller-ref changes run the old ref's cleanup before assigning the new ref.
  • Server rendering attaches no controller.
  • After an accepted press, the matching pointer click is default-prevented while propagation continues.
  • Touch, pen, keyboard, and modified clicks keep their native behavior and do not call navigate.

Core owns eligibility, click suppression, and event ordering. Add data-no-fast-link to an anchor or descendant when the browser must retain the interaction.

  • @fast-link/core documents eligibility, controller options, and adapter decisions.
  • @fast-link/next provides App Router components and a custom-renderer hook.

版本与发布记录

@fast-link/react 共发布 3 个版本,首次发布于 2026/10/07;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.2latest2026/11/0311 天前
0.0.12026/10/2717 天前
0.0.02026/10/071 个月前

关于 @fast-link/react 的常见问题

@fast-link/react 是什么?

React lifecycle binding for Fast Link DOM controllers

如何安装 @fast-link/react?

在项目目录运行 npm install @fast-link/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=19 <20)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fast-link/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fast-link/react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fast-link/react 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fast-link/react 可以商用吗?

@fast-link/react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fast-link/react 的下载量有多少?

根据 npm 官方下载统计,@fast-link/react 在 2026/10/15 – 2026/11/13 这 30 天内被下载 10,036 次,日均约 335 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fast-link/react 还在维护吗?

最新版本 0.0.2 发布于 2026/11/03(11 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/10/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。