@usefy/use-previous
v0.25.1
Published
A React hook that returns the value from the previous render
Maintainers
Readme
Overview
usePrevious is part of the @usefy ecosystem — a collection of production-ready, TypeScript-first, SSR-safe React hooks. It returns the previous distinct value: the value from the last render in which it actually changed (compared with Object.is by default), not simply the value one render ago. If a value is unchanged across a re-render, the returned "previous" does not advance — the behavior change-detection logic usually wants. Pass a custom comparator to treat referentially-different-but-equal values as unchanged.
Features
- Ref-based — no extra re-render to track the previous value
- Distinct tracking — a value that never changes keeps
undefined - Optional comparator — ignore new-but-equal values (defaults to
Object.is) - TypeScript-first — full type inference and exported types
- Tiny & tree-shakeable — zero dependencies, published as its own package
Installation
# npm
npm install @usefy/use-previous
# yarn
yarn add @usefy/use-previous
# pnpm
pnpm add @usefy/use-previousRequires React 18 or 19 (peerDependencies: "react": "^18.0.0 || ^19.0.0").
Quick Start
import { usePrevious } from "@usefy/use-previous";
const [count, setCount] = useState(0);
const prev = usePrevious(count); // undefined, then the previous valueAPI
usePrevious<T>(value, isEqual?): T | undefined — tracks the previous distinct value; pass isEqual (defaults to Object.is) to ignore new-but-equal values.
Testing
📊 View Detailed Coverage Report (GitHub Pages) — 6 tests, 100% statement coverage.
License
MIT © mirunamu
This package is part of the usefy monorepo.
