A React hook that updates useRef().current with the most recent value each invocation
npm install @react-hook/latestnpm i @react-hook/latest
A React hook that updates useRef().current with the most recent value each invocation
``jsx harmony
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])
}
`
`ts`
const useLatest:
| Prop | Type | Required? | Description |
| ----- | --------------- | --------- | ------------------------- |
| value | T extends any` | Yes | Any value you want stored |
MIT