Skip to content

usePrevious

Returns the value from the previous render. Useful for comparing values.

import { usePrevious } from 'asterui'
function CounterExample() {
const [count, setCount] = useState(0)
const prevCount = usePrevious(count)
return (
<div>
<p>Current: {count}</p>
<p>Previous: {prevCount ?? 'N/A'}</p>
<Button onClick={() => setCount(c => c + 1)}>Increment</Button>
</div>
)
}
ParameterTypeDescription
valueTCurrent value to track
TypeDescription
T | undefinedPrevious value (undefined on first render)