@usefy/use-latest
v0.25.1
Published
A React hook that returns a ref always holding the latest value
Maintainers
Readme
Overview
useLatest is part of the @usefy ecosystem — a collection of production-ready, TypeScript-first, SSR-safe React hooks. Returns a ref that always holds the latest value.
Features
- Stable identity — safe as an effect dependency
- No stale closures — long-lived callbacks read the newest value
- Synchronous —
.currentupdates on every render - TypeScript-first — full type inference and exported types
- Tiny & tree-shakeable — zero dependencies, published as its own package
Installation
# npm
npm install @usefy/use-latest
# yarn
yarn add @usefy/use-latest
# pnpm
pnpm add @usefy/use-latestRequires React 18 or 19 (peerDependencies: "react": "^18.0.0 || ^19.0.0").
Quick Start
import { useLatest } from "@usefy/use-latest";
const latest = useLatest(value);
// read latest.current inside stable callbacks / listenersAPI
useLatest<T>(value): { readonly current: T } — a stable ref whose current is updated to the latest value on every render.
Testing
📊 View Detailed Coverage Report (GitHub Pages) — 3 tests, 100% statement coverage.
License
MIT © mirunamu
This package is part of the usefy monorepo.
