usehooks-ts
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
-
TypeError: require is not a function
cause Attempting to import `usehooks-ts` modules using CommonJS `require()` syntax after the library migrated to ES Modules in v3.0.0.fixUpdate your import statements to use ES Module syntax: `import { HookName } from 'usehooks-ts'`. -
Property 'useDeprecatedHookName' does not exist on type 'typeof import("usehooks-ts")'.cause Trying to use a hook that was explicitly removed in `usehooks-ts` v3.0.0 or a later version after being deprecated.fixRefer to the `usehooks-ts` documentation to identify the recommended replacement hook (e.g., `useScrollLock` for `useLockedBody`) or implement the desired functionality manually. -
Type 'MyComponentProps' is not assignable to type 'HookParameterType'. Argument of type 'string' is not assignable to parameter of type 'number'.
cause This usually indicates a type mismatch in arguments passed to hooks, often exacerbated by the library's TypeScript-first approach and strict typing, or changes to internal type aliases in v3.0.0.fixCarefully review the type signature of the hook you are using in the `usehooks-ts` documentation. Ensure the values and types you are passing as arguments exactly match the expected types.
Warnings
- breaking Version 3.0.0 of `usehooks-ts` moved its entire workspace to ES Modules. This means direct CommonJS `require()` statements will no longer work, requiring projects to update their import syntax to ES Module standards.
- breaking In version 3.0.0, several previously deprecated hooks and their signatures were completely removed. Projects upgrading from versions prior to 3.0.0 might encounter runtime errors if these hooks are still in use.
- breaking Version 3.0.0 introduced changes preferring `type` over `interface` for some internal types and renamed or made private certain type aliases. This may cause TypeScript compilation errors in applications that directly reference or extend `usehooks-ts` internal types.
- deprecated Several hooks have been deprecated in recent minor versions, with some replaced by new alternatives. For example, `useLockedBody` was replaced by `useScrollLock` (v2.15.0), and `useFetch`, `useEffectOnce`, `useIsFirstRender`, and `useUpdateEffect` were deprecated in v2.14.0.
- gotcha While version 3.1.1 officially adds React 19 to its `peerDependencies`, users on older versions of `usehooks-ts` might encounter peer dependency warnings or potential compatibility issues when using React 19.
Install
-
npm install usehooks-ts -
yarn add usehooks-ts -
pnpm add usehooks-ts
Imports
- useLocalStorage
const useLocalStorage = require('usehooks-ts').useLocalStorageimport { useLocalStorage } from 'usehooks-ts' - useBoolean
import useBoolean from 'usehooks-ts/useBoolean'
import { useBoolean } from 'usehooks-ts' - useDocumentTitle
import * as usehooks from 'usehooks-ts'
import { useDocumentTitle } from 'usehooks-ts'
Quickstart
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;