usePrevious
Track the previous value of a variable.
The usePrevious
hook is helpful when you want to keep track of the previous value of a variable. This comes in handy when you need to compare the new and previous value to trigger an action.
usePrevious
Previous Value: -1
Current Value: 0
Usage
Import the hook from @abhushanaj/react-hooks
and use in required component.
If no second argument is passed to the usePrevious
hook, then on the first render it will return undefined
API Reference
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
value | T | The variable to keep track of. | N/A |
defaultValue | U (optional) | The default value for the first render. | undefined |
Return Value
Parameter | Type | Description | Default |
---|---|---|---|
previousValue | R | The previous value of the value T . | undefined |
Types Reference
T
: extendsunknown
U
: extendsT | undefined
R
:U extends T ? T : undefined