usehooks-ts

3.1.1 · active · verified Sun Apr 19

usehooks-ts is a comprehensive, TypeScript-first React hook library designed to accelerate application development by providing a collection of ready-to-use, well-tested custom hooks. As of its current stable version, 3.1.1, the library maintains an active development pace with frequent patch and minor releases, alongside significant major updates like the recent v3.0.0. Key differentiators include its strict adherence to TypeScript, ensuring type safety and improved developer experience, and its focus on being fully tree-shakable (utilizing ES Modules since v3) to minimize bundle size. It aims to embody DRY principles, offering solutions for common React patterns such as state management, DOM manipulation, event handling, and more, making it a robust choice for modern React projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic usage of `useLocalStorage` to persist and manage a counter across browser sessions.

import React from 'react';
import { useLocalStorage } from 'usehooks-ts';

function MyComponent() {
  // Initialize a piece of state in localStorage with a default value of 0
  const [count, setCount] = useLocalStorage('my-counter-key', 0);

  const increment = () => setCount(prev => prev + 1);
  const decrement = () => setCount(prev => prev - 1);
  const reset = () => setCount(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={decrement}>Decrement</button>
      <button onClick={reset}>Reset</button>
    </div>
  );
}

export default MyComponent;

view raw JSON →