@gilbarbara/hooks

中文文档
0.11.0发布于 2026/02/07MITTypeScriptESM + CJS构建来源已验证近 30 天下载 448.5万

Collection of useful React hooks

原始使用文档
文档目录17 节
  1. Setup
  2. Features
  3. Example
  4. Hooks
  5. React Hooks with Deep Comparison
  6. State
  7. Effects
  8. Lifecycles
  9. Refs and DOM
  10. UI and Interactions
  11. Performance and Optimization
  12. Timers
  13. Data Fetching
  14. Debugging and Development
  15. Utilities
  16. ESLint Configuration
  17. License

@gilbarbara/hooks

npm version CI Quality Gate Status Coverage

A collection of React hooks designed to simplify state management, side effects, and UI interactions.

Setup

npm i @gilbarbara/hooks

Requires React 16.8+ (Hooks support). TypeScript support is included.

Features

  • Custom versions of useEffect, useCallback, and useMemo with deep comparison.
  • Built-in debouncing and throttling for smooth performance (useDebounce, useThrottle).
  • Advanced state management (useSetState, useToggle, usePersistentState).
  • Debugging tools to optimize re-renders (useDataChanges, useRenderCount).
  • Flexible API integrations (useFetch with retries and backoff support).

Example

Here's an example of using useToggle, useThrottle, and useFetch together:

import { useToggle, useThrottle, useFetch } from '@gilbarbara/hooks';

function Component() {
  const [isEnabled, { toggle }] = useToggle(false);
  const throttledFetch = useThrottle(() => {
    fetch('/api/data');
  }, 1000);
  const { data, error } = useFetch('/api/data');

  return (
    <div>
      <button onClick={toggle}>{isEnabled ? 'Disable' : 'Enable'}</button>
      <button onClick={throttledFetch}>Fetch Data</button>
      {error ? <p>Error: {error.message}</p> : <p>Data: {JSON.stringify(data)}</p>}
    </div>
  );
}

Hooks

React Hooks with Deep Comparison

Custom React's built-in hooks deep comparison on their dependencies.

useCallbackDeepCompare — A custom useCallback with deep comparison.
useEffectDeepCompare — A custom useEffect with deep comparison.
useMemoDeepCompare — A custom useMemo with deep comparison.

State

Hooks for managing and persisting application state.

usePersistentState — State hook that persists the state in localStorage.
useSetState — Returns a setState that merges object changes into the current state.
useToggle — State hook to track the value of a boolean.

Effects

Hooks for managing side effects and extending React’s useEffect.

useEffectOnce — Execute the effect only once.
useHasChanged — Detect value changes and optionally trigger a callback.
useIsomorphicLayoutEffect — Use useLayoutEffect on the client and useEffect on the server.
useUpdateEffect — A custom useEffect that doesn’t run on mount.

Lifecycles

Hooks for managing component lifecycle events such as mounting and unmounting.

useMount — Execute a callback when the component is mounted.
useUnmount — Execute a callback when the component is unmounted.
useLifecycleHooks — Execute the callbacks when the component mount and unmount.
useIsMounted — Check if the component is still mounted.
useIsFirstRender — Check if it’s the first mount.

Refs and DOM

Hooks for managing refs and interacting with the DOM.

useLatest — Get a ref containing the most recent value.
useMergeRefs — Merge multiple refs into one.
usePrevious — Track the previous value of a variable.

UI and Interactions

Hooks for managing user interactions and responsive design.

useBreakpoint — Get responsive breakpoints for adaptive layouts.
useClickOutside — Execute the callback when clicking outside the target element.
useElementMeasure — Get element dimensions using the ResizeObserver API.
useMediaQuery — Detect media query changes.
useIntersectionObserver — Detects the visibility of an element on the viewport using the IntersectionObserver API.
useResizeObserver — Get element dimensions using the ResizeObserver API.
useWindowSize — Get the window dimensions. Updates on resize.

Performance and Optimization

Hooks for optimizing performance by reducing unnecessary renders or controlling execution frequency.

useDebounce — Defer function execution until the delay has elapsed since the last invocation.
useMemoizedValue — Get a stabilized value that only updates when the original value is truly different.
useThrottle — Return a throttled function that invokes fn once per every ms.
useThrottleValue — Return a throttled value that changes only once per every ms.

Timers

Hooks for managing time-based operations.

useInterval — Execute the callback repeatedly with the specified delay.
useTimeout — Execute the callback after the specified delay.

Data Fetching

Hooks for working with APIs and third-party scripts.

useFetch — Make a request with fetch. It supports dynamic URLs, caching, retries, and much more.
useScript — Dynamically load a script tag and append it to the document.body.

Debugging and Development

Hooks for debugging, monitoring, and optimizing component behavior.

useDataChanges — Detect which prop/state changes are causing a component to re-render.
useRenderCount — Log how many times the component has rendered.
useUpdate — Return a function that re-renders the component when called.

Utilities

useLocalStorage — Interact with the browser’s localStorage API.
useLocation — Track the browser’s location.
useOnce — Execute code just once before the component renders.

ESLint Configuration

To take full advantage of hooks with dependencies, add the following rule to your ESLint config:

{
  "rules": {
    "react-hooks/exhaustive-deps": [
      "warn", {
        "additionalHooks": "(useDebounce|useUpdateEffect|use.*DeepCompare)"
      }
    ]
  }
}

License

MIT

版本与发布记录

@gilbarbara/hooks 共发布 27 个版本,首次发布于 2020/03/06;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.11.0latest2026/02/079 个月前
0.10.52026/02/049 个月前
0.10.42025/02/161.7 年前
0.10.32025/02/161.7 年前
0.10.22025/02/081.8 年前
0.10.12025/02/031.8 年前

关于 @gilbarbara/hooks 的常见问题

@gilbarbara/hooks 是什么?

Collection of useful React hooks

如何安装 @gilbarbara/hooks?

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

@gilbarbara/hooks 支持 TypeScript 吗?

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

@gilbarbara/hooks 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gilbarbara/hooks 可以商用吗?

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

@gilbarbara/hooks 的下载量有多少?

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

@gilbarbara/hooks 还在维护吗?

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