usePrevious
Tracks the value from the previous render / tracked update. Returns undefined on the first run.
import { useEffect } from 'react'
import { usePrevious } from '@wire-ui/react'
function Counter({ count }: { count: number }) {
const previous = usePrevious(count)
useEffect(() => {
if (previous !== undefined && count > previous) {
console.log(`went up by ${count - previous}`)
}
}, [count, previous])
return <span>{count}</span>
}Parameters
| Param | Type | Description |
|---|---|---|
value | T | Value to track. |
Returns
T | undefined — the value from the previous render, or undefined before the first update.
Last updated on