DEMO
Current Count is - 0
Previous Count is -
usePrevious
Returns the value of the argument from the previous render
Usage
import { useState } from 'react'
import { usePrevious } from '@react-hooks-library/core'
export function Demo() {
const [count, setCount] = useState(0)
const previousCount = usePrevious(count)
return (
<div>
<div>Current Count is - {count}</div>
<div>Previous Count is - {previousCount}</div>
<button onClick={() => setCount((c) => c + 1)}>Increment</button>
</div>
)
}
Type Declarations
declare function usePrevious<T>(value: T): T | undefined;