Configs2026年7月16日·1 分钟阅读

ahooks — High-Quality React Hooks Library by Alibaba

A comprehensive collection of production-ready React hooks covering state management, side effects, DOM manipulation, and advanced patterns. Provides over 60 hooks with full TypeScript support and SSR compatibility.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
ahooks
先审查命令
npx -y tokrepo@latest install 8844cd48-80f5-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

ahooks is a React hooks library developed by the Ant Group (Alibaba) that provides a curated set of over 60 hooks for common development patterns. Rather than writing custom hooks for debouncing, request management, or DOM events, teams can import battle-tested implementations that handle edge cases, cleanup, and TypeScript types out of the box.

What ahooks Does

  • Provides useRequest for declarative data fetching with caching, polling, and retry
  • Offers state hooks like useLocalStorageState, useSessionStorageState, and useMap
  • Includes DOM hooks for click-outside detection, intersection observer, and resize tracking
  • Delivers effect hooks for debounce, throttle, interval, timeout, and lock patterns
  • Supports SSR environments with safe window/document access guards

Architecture Overview

ahooks is organized into hook categories: Async (useRequest), State, Effect, DOM, and Advanced. Each hook is a self-contained module with its own tests, types, and documentation. The library uses a monorepo structure with independent compilation so that tree-shaking works at the hook level. useRequest — the most complex hook — implements a plugin architecture internally, allowing features like caching, polling, debounce, and error retry to compose independently.

Self-Hosting & Configuration

  • Install via npm or yarn — no configuration files needed
  • Import individual hooks for automatic tree-shaking in any modern bundler
  • Configure useRequest defaults (polling interval, cache time, retry count) via options
  • Wrap your app in a ConfigProvider for global defaults across all ahooks instances
  • Works with React 16.8+ and supports React 18 concurrent features

Key Features

  • useRequest with built-in loading states, caching, polling, pagination, and SWR-like revalidation
  • useDebounce and useThrottle that handle value and callback variants with proper cleanup
  • useVirtualList for rendering large lists with smooth scrolling and minimal DOM nodes
  • Full TypeScript generics so return types infer correctly from input parameters
  • Comprehensive test coverage and documentation with live examples for every hook

Comparison with Similar Tools

  • React Use — large collection of community hooks; ahooks is more curated with consistent API design and TypeScript quality
  • SWR — Vercel's data fetching library focused on the stale-while-revalidate pattern; ahooks' useRequest covers similar ground plus pagination and manual triggers
  • TanStack Query (React Query) — the most popular async state manager; more powerful for complex caching but ahooks bundles state, DOM, and effect hooks alongside
  • usehooks-ts — TypeScript-first hook collection; smaller set with simpler implementations compared to ahooks' production-hardened hooks
  • Mantine Hooks — hooks bundled with the Mantine UI library; ahooks is UI-library agnostic

FAQ

Q: Does ahooks work with Next.js and SSR? A: Yes. ahooks guards against server-side window/document access and works in SSR frameworks like Next.js.

Q: Can I use just one hook without importing the entire library? A: Yes. Each hook is a separate module, so modern bundlers tree-shake unused hooks automatically.

Q: How does useRequest compare to React Query? A: useRequest covers common data fetching patterns (loading, error, caching, polling) in a simpler API. React Query offers more advanced features like query invalidation and infinite queries.

Q: Is ahooks actively maintained? A: Yes. The library is maintained by the Ant Group open-source team and receives regular updates.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产