@react-hook/latest
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

useLatest()

Bundlephobia Types Build status NPM Version MIT License

npm i @react-hook/latest

A React hook that updates useRef().current with the most recent value each invocation

Quick Start

import useLatest from '@react-hook/latest'

const useEvent = (element, name, listener) => {
  const latest = useLatest(listener)

  React.useEffect(() => {
    const listen = (e) => latest.current(e)
    element.addEventListener(name, listen)
    return () => element.removeEventListener(name, listen)
  }, [latest])
}

API

useLatest(value)

const useLatest: <T extends any>(current: T) => React.MutableRefObject<T>

Props

Prop Type Required? Description
value T extends any Yes Any value you want stored

Returns React.MutableRefObject<T>

LICENSE

MIT

Package Sidebar

Install

npm i @react-hook/latest

Weekly Downloads

493,290

Version

1.0.3

License

MIT

Unpacked Size

12.9 kB

Total Files

24

Last publish

Collaborators

  • jaredlunde