useLatest
A React Hook that returns a ref to the latest value. It can be useful to prevent stale state in timer callback or async context.
Demo
Usage
const latest = useLatest(count)
console.log(latest.current) // latest count
const latest = useLatest({ callback, options })
console.log(latest.current.callback) // latest callback
console.log(latest.current.options) // latest options
Source
Click links below to view source on GitHub.
API
const latest = useLatest(value)
Value
Any value that you want to keep track of.
Retruns
A React ref (React.MutableRefObject<T>
)