# @lilib/hooks

> A collection of practical React hooks covering state, effects, refs, async data and browser APIs, with TypeScript, SSR and StrictMode support.

Latest version **2.4.0** (published 2026-11-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lilib/hooks
pnpm add @lilib/hooks
yarn add @lilib/hooks
bun add @lilib/hooks
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.4.0 |
| Published | 2026-11-16 |
| First published | 2021-03-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 12 |
| Unpacked size | 1.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | 李蔚生 |
| Maintainers | airquality |
| Keywords | react, hooks, react-hooks, typescript, ssr, strict-mode, async, debounce, throttle, animation, storage |

## Links

- npm: https://www.npmjs.com/package/@lilib/hooks
- Repository: https://github.com/lilibraries/hooks
- Homepage: https://lilibraries.github.io/hooks/
- Issues: https://github.com/lilibraries/hooks/issues
- npm.io page: https://npm.io/package/@lilib/hooks

## Dependencies (12)

- [raf](https://npm.io/package/raf.md) ^3.4.1
- [lodash](https://npm.io/package/lodash.md) ^4.18.1
- [js-cookie](https://npm.io/package/js-cookie.md) ^3.0.8
- [lru-cache](https://npm.io/package/lru-cache.md) ^10.4.3
- [@types/raf](https://npm.io/package/@types/raf.md) ^3.4.3
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.17.26
- [eventemitter3](https://npm.io/package/eventemitter3.md) ^5.0.4
- [@types/js-cookie](https://npm.io/package/@types/js-cookie.md) ^3.0.6
- [requestidlecallback](https://npm.io/package/requestidlecallback.md) ^0.3.0
- [intersection-observer](https://npm.io/package/intersection-observer.md) ^0.12.2
- [@babel/runtime-corejs3](https://npm.io/package/@babel/runtime-corejs3.md) ^7.29.10
- [resize-observer-polyfill](https://npm.io/package/resize-observer-polyfill.md) ^1.5.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.4.0 (latest) — 2026-11-16
- 2.3.0 — 2026-11-12
- 2.2.0 — 2026-11-11
- 2.1.0 — 2026-11-09
- 2.0.1 — 2026-10-23
- 2.0.0 — 2026-10-19
- 1.1.0 — 2025-04-07
- 1.0.3 — 2023-10-17
- 1.0.0 — 2022-12-25
- 0.3.1 — 2022-12-24
- 0.3.0 — 2022-12-22
- 0.2.1 — 2022-06-28
- 0.2.0 — 2022-06-27
- 0.1.1 — 2021-03-30
- 0.1.0 — 2021-03-29

## README

# @lilib/hooks

[![npm version](https://img.shields.io/npm/v/@lilib/hooks?style=flat-square)](https://www.npmjs.com/package/@lilib/hooks)
[![npm downloads](https://img.shields.io/npm/dm/@lilib/hooks?style=flat-square)](https://www.npmjs.com/package/@lilib/hooks)
[![license](https://img.shields.io/npm/l/@lilib/hooks?style=flat-square)](https://github.com/lilibraries/hooks/blob/master/LICENSE)

A collection of practical React hooks covering async data, states, effects, callbacks, refs and browser APIs — written in TypeScript, compatible with React 16.8+, 17, 18 and 19, and safe for SSR and StrictMode.

- [Documentation](https://lilibraries.github.io/hooks/) — full API reference with live examples
- [llms.txt](https://lilibraries.github.io/hooks/llms.txt) — machine-readable index for AI assistants and LLM tooling
- [Changelog](https://github.com/lilibraries/hooks/blob/master/CHANGELOG.md)
- [Report an issue](https://github.com/lilibraries/hooks/issues)

## Features

- More than 50 hooks covering async data, effects, states, callbacks, refs and browser APIs.
- Works with React 16.8+, 17, 18 and 19.
- SSR-safe — graceful fallbacks outside the browser.
- StrictMode-safe — idempotent effects with proper cleanup.
- Fully typed — written in TypeScript.
- Ships ESM, CJS and UMD builds.
- Tree-shakeable — bundlers can drop unused hooks from the ESM build.

## Install

```shell
pnpm add @lilib/hooks
# Or
npm install @lilib/hooks
```

## Usage

```tsx
import { useLoad } from "@lilib/hooks";

function UserProfile({ userId }: { userId: string }) {
  const { data, error, loading } = useLoad(
    () => fetch(`/users/${userId}`).then((response) => response.json()),
    [userId],
  );

  if (loading) {
    return <div>Loading...</div>;
  }

  if (error) {
    return <div>Something went wrong.</div>;
  }

  return <div>Hello, {data.name}!</div>;
}
```

See the [documentation](https://lilibraries.github.io/hooks/) for the full API reference and live examples of every hook.

## Hooks

### Async

| Hook                                                         | Description                                                |
| ------------------------------------------------------------ | ---------------------------------------------------------- |
| [`useLoad`](https://lilibraries.github.io/hooks/useLoad)     | Load async data, typically for network requests.           |
| [`useReload`](https://lilibraries.github.io/hooks/useReload) | Reload all `useLoad` requests that share the same `key`.   |
| [`useSubmit`](https://lilibraries.github.io/hooks/useSubmit) | Submit network requests such as create, update and delete. |

### Effects

| Hook                                                                                         | Description                                                                                            |
| -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| [`useMount`](https://lilibraries.github.io/hooks/useMount)                                   | Run an effect after the component is mounted.                                                          |
| [`useUpdate`](https://lilibraries.github.io/hooks/useUpdate)                                 | Run an effect after the component updates (the initial mount is skipped).                              |
| [`useUnmount`](https://lilibraries.github.io/hooks/useUnmount)                               | Run an effect when the component unmounts.                                                             |
| [`useRefEffect`](https://lilibraries.github.io/hooks/useRefEffect)                           | Like `React.useEffect`, but ReactRefs in the deps are dereferenced to `ref.current` before comparison. |
| [`useRefUpdate`](https://lilibraries.github.io/hooks/useRefUpdate)                           | Like `useUpdate`, but ReactRefs in the deps are dereferenced to `ref.current` before comparison.       |
| [`useLayoutMount`](https://lilibraries.github.io/hooks/useLayoutMount)                       | Like `useMount`, but runs before the browser paints.                                                   |
| [`useLayoutUpdate`](https://lilibraries.github.io/hooks/useLayoutUpdate)                     | Like `useUpdate`, but runs before the browser paints.                                                  |
| [`useLayoutRefEffect`](https://lilibraries.github.io/hooks/useLayoutRefEffect)               | Like `useRefEffect`, but runs before the browser paints.                                               |
| [`useLayoutRefUpdate`](https://lilibraries.github.io/hooks/useLayoutRefUpdate)               | Like `useRefUpdate`, but runs before the browser paints.                                               |
| [`useIsomorphicLayoutEffect`](https://lilibraries.github.io/hooks/useIsomorphicLayoutEffect) | `React.useLayoutEffect` in the DOM, `React.useEffect` during SSR.                                      |

### States

| Hook                                                                               | Description                                                                                                     |
| ---------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| [`useCache`](https://lilibraries.github.io/hooks/useCache)                         | Cache data for a certain period of time, with optional sync between components sharing the same cache instance. |
| [`useToggle`](https://lilibraries.github.io/hooks/useToggle)                       | Manage a boolean value.                                                                                         |
| [`useCountdown`](https://lilibraries.github.io/hooks/useCountdown)                 | Track the remaining time until a deadline, with pause and resume.                                               |
| [`useRafState`](https://lilibraries.github.io/hooks/useRafState)                   | Like `React.useState`, but updates within an animation frame are merged into a single commit.                   |
| [`useSetState`](https://lilibraries.github.io/hooks/useSetState)                   | Like `this.setState` of class components, merging partial state updates.                                        |
| [`useListState`](https://lilibraries.github.io/hooks/useListState)                 | Manage an array state with positional operations, reordering and value-based removal.                           |
| [`useSafeState`](https://lilibraries.github.io/hooks/useSafeState)                 | Like `React.useState`, but setting state after unmount is safe.                                                 |
| [`useControllableState`](https://lilibraries.github.io/hooks/useControllableState) | Manage a value that can be either controlled or uncontrolled.                                                   |
| [`useDelayedValue`](https://lilibraries.github.io/hooks/useDelayedValue)           | Delay updates to a value.                                                                                       |
| [`useMemoizedValue`](https://lilibraries.github.io/hooks/useMemoizedValue)         | Return the previous value until the value actually changes (deep comparison).                                   |
| [`useThrottledValue`](https://lilibraries.github.io/hooks/useThrottledValue)       | Throttle a value.                                                                                               |
| [`useDebouncedValue`](https://lilibraries.github.io/hooks/useDebouncedValue)       | Debounce a value.                                                                                               |

### Callbacks

| Hook                                                             | Description                                                            |
| ---------------------------------------------------------------- | ---------------------------------------------------------------------- |
| [`useRaf`](https://lilibraries.github.io/hooks/useRaf)           | Invoke a callback before the browser repaints.                         |
| [`useIdle`](https://lilibraries.github.io/hooks/useIdle)         | Invoke a callback when the browser is idle.                            |
| [`useAnimate`](https://lilibraries.github.io/hooks/useAnimate)   | Run JavaScript animations.                                             |
| [`useTimeout`](https://lilibraries.github.io/hooks/useTimeout)   | Execute a callback after a delay.                                      |
| [`useInterval`](https://lilibraries.github.io/hooks/useInterval) | Run a callback repeatedly at a fixed interval.                         |
| [`usePersist`](https://lilibraries.github.io/hooks/usePersist)   | Keep a stable function identity while always calling the latest logic. |
| [`useRerender`](https://lilibraries.github.io/hooks/useRerender) | Re-render the component manually, usually for mutable data.            |
| [`useThrottle`](https://lilibraries.github.io/hooks/useThrottle) | Use a throttled callback.                                              |
| [`useDebounce`](https://lilibraries.github.io/hooks/useDebounce) | Use a debounced callback.                                              |

### Refs

| Hook                                                                     | Description                                                                                    |
| ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- |
| [`useLatestRef`](https://lilibraries.github.io/hooks/useLatestRef)       | A ref that always points to the latest value, avoiding stale closures in long-lived callbacks. |
| [`usePreviousRef`](https://lilibraries.github.io/hooks/usePreviousRef)   | A ref that points to the value from the previous update.                                       |
| [`useComposedRef`](https://lilibraries.github.io/hooks/useComposedRef)   | Compose multiple refs (objects or callbacks) into one.                                         |
| [`useMountedRef`](https://lilibraries.github.io/hooks/useMountedRef)     | A ref that tells whether the component is mounted.                                             |
| [`useUnmountedRef`](https://lilibraries.github.io/hooks/useUnmountedRef) | A ref that tells whether the component is unmounted.                                           |

### Browser

| Hook                                                                                     | Description                                                                            |
| ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| [`useTitle`](https://lilibraries.github.io/hooks/useTitle)                               | Set the document title.                                                                |
| [`useFavicon`](https://lilibraries.github.io/hooks/useFavicon)                           | Set the document favicon.                                                              |
| [`useOnline`](https://lilibraries.github.io/hooks/useOnline)                             | Detect whether the network is online.                                                  |
| [`useDarkMode`](https://lilibraries.github.io/hooks/useDarkMode)                         | Detect whether the system prefers a dark color scheme.                                 |
| [`useMatchMedia`](https://lilibraries.github.io/hooks/useMatchMedia)                     | Determine whether the document matches a media query.                                  |
| [`usePageVisible`](https://lilibraries.github.io/hooks/usePageVisible)                   | Detect whether the page is visible, including bfcache restores.                        |
| [`useWindowFocus`](https://lilibraries.github.io/hooks/useWindowFocus)                   | Detect whether the window has focus.                                                   |
| [`useHotkey`](https://lilibraries.github.io/hooks/useHotkey)                             | Bind a keyboard shortcut to a callback.                                                |
| [`useKeyPressed`](https://lilibraries.github.io/hooks/useKeyPressed)                     | Detect whether a key is currently held down.                                           |
| [`useClickOutside`](https://lilibraries.github.io/hooks/useClickOutside)                 | Trigger a callback when clicking outside the target area.                              |
| [`useEventListener`](https://lilibraries.github.io/hooks/useEventListener)               | Set up an event listener on the target.                                                |
| [`useScroll`](https://lilibraries.github.io/hooks/useScroll)                             | Track the scroll position, direction, and progress of the page or an element.          |
| [`useScrollLock`](https://lilibraries.github.io/hooks/useScrollLock)                     | Lock scrolling of the page or an element.                                              |
| [`useWindowSize`](https://lilibraries.github.io/hooks/useWindowSize)                     | Get the window size.                                                                   |
| [`useElementSize`](https://lilibraries.github.io/hooks/useElementSize)                   | Get the size of a DOM element.                                                         |
| [`useIntersecting`](https://lilibraries.github.io/hooks/useIntersecting)                 | Detect whether an element intersects the viewport or a specified root.                 |
| [`useResizeObserver`](https://lilibraries.github.io/hooks/useResizeObserver)             | Observe an element's size changes.                                                     |
| [`useMutationObserver`](https://lilibraries.github.io/hooks/useMutationObserver)         | Observe changes to a DOM node, such as inserted or removed child nodes and attributes. |
| [`useIntersectionObserver`](https://lilibraries.github.io/hooks/useIntersectionObserver) | Observe changes in an element's intersection with another element or the viewport.     |
| [`useCookie`](https://lilibraries.github.io/hooks/useCookie)                             | Get, set and/or remove cookies.                                                        |
| [`useLocalStorage`](https://lilibraries.github.io/hooks/useLocalStorage)                 | Get, set and/or remove `localStorage`.                                                 |
| [`useSessionStorage`](https://lilibraries.github.io/hooks/useSessionStorage)             | Get, set and/or remove `sessionStorage`.                                               |

## Changelog

See [CHANGELOG.md](https://github.com/lilibraries/hooks/blob/master/CHANGELOG.md) for the full release history.

## Contributing

Contributions are welcome! Feel free to [open an issue](https://github.com/lilibraries/hooks/issues) or submit a pull request.

## License

[MIT](./LICENSE)

---
_Source: https://npm.io/package/@lilib/hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
