useHooks logo

useHooks

Modern, server-safe React Hooks collection by the ui.dev team.

npm install use-hooks
11.2K2.7K/weekv2.0.0-rc.560.56 KBMIT79 issues
Last updated: 2019-10-11
Star history chart for uidotdev/usehooks

TL;DR

A curated collection of modern, server-safe React Hooks built by the ui.dev team.

Focuses on high-quality implementations of essential utilities, ensuring compatibility with Server-Side Rendering (SSR) and modern bundlers.

Why useHooks?

Created by Tyler McGinnis and the team at ui.dev, useHooks represents a modern reboot of the "utility hooks" concept. While older libraries often break in Server-Side Rendering (SSR) environments or rely on outdated patterns, useHooks is built for the Next.js and Remix era.

  • Server-Safe by Default: Every hook is written to handle SSR environments gracefully, checking for window or DOM availability to prevent hydration mismatches.
  • Curated Quality: Unlike "kitchen sink" libraries that contain hundreds of unmaintained hooks, this library focuses on the most essential 30-40 utilities, keeping quality high.
  • Modern Standards: Built with modern React best practices, ensuring tree-shakability and proper dependency array management.
  • Interactive Documentation: The official documentation (usehooks.com) is widely regarded as some of the best in the ecosystem, offering live, interactive examples.

Code Snippet

The useIntersectionObserver hook abstracts the complex browser API into a simple React interface, perfect for lazy loading images or triggering animations on scroll.

import * as React from "react";
import { useIntersectionObserver } from "@uidotdev/usehooks";

export default function ScrollComponent() {
  const [ref, entry] = useIntersectionObserver({
    threshold: 0,
    root: null,
    rootMargin: "0px",
  });

  return (
    <section>
      <figure ref={ref}>
        {entry?.isIntersecting ? (
          <img src="https://placekitten.com/800/400" alt="Lazy loaded" />
        ) : (
          <div style={{ height: 400, background: '#f0f0f0' }}>
            Loading placeholder...
          </div>
        )}
        <figcaption>
           Status: {entry?.isIntersecting ? "Visible" : "Hidden"}
        </figcaption>
      </figure>
    </section>
  );
}

This pattern removes the boilerplate of creating the observer instance, connecting it in useEffect, and cleaning it up manually.

Pros and Cons

No library is perfect; understanding the trade-offs is key to selecting the right tool.

Pros

  • SSR Compatibility: One of the few utility libraries that guarantees safety in Next.js/Remix/Gatsby server environments out of the box.
  • Developer Experience: Excellent TypeScript definitions and documentation make it easy to adopt.
  • Tree-Shakable: You only pay for the hooks you use, keeping bundle sizes minimal.

Cons

  • Limited Scope: It purposefully avoids complex business logic (like data fetching or form state), focusing only on browser/UI utilities.
  • Newer Entrant: Has fewer total hooks compared to the massive (but aging) react-use library.
  • Dependency: While small, it introduces a runtime dependency, whereas some developers prefer copy-pasting code snippets (like usehooks-ts).

Comparison with Other Hooks Libraries

The table below outlines the positioning differences between useHooks and other popular utility libraries:

LibraryDesign PhilosophyBest ForPain Points
useHooks"Modern & Safe"
Server-safe, essential browser utilities.
Next.js/Remix Apps
Modern full-stack React applications needing reliable browser APIs.
Scope
Does not include complex state or data management tools.
react-use"The Kitchen Sink"
Huge collection of every possible utility.
Legacy Projects
Older apps where bundle size is less of a concern.
Deprecation
Contains outdated patterns and is often not SSR-safe.
ahooks"Enterprise Ready"
Heavy focus on async state (useRequest) and complex logic.
Complex SaaS
Apps needing robust request management and advanced UI logic.
Overkill
Too heavy if you just need a useLocalStorage.

Verdict: When to Adopt

Choose useHooks if you are building a modern application (especially with Next.js or Remix) and need a reliable set of standard browser utilities (like useLocalStorage, useMediaQuery, useWindowSize) without worrying about hydration errors or abandoned code. It is the spiritual successor to react-use for the modern web.