usePrevious
The usePrevious
hook allows you to capture and use the previous value of a state or prop in a React component. This can be particularly useful when you need to compare the current value with its previous state, such as implementing animations, tracking changes, or conditionally executing logic based on value changes.
Usage
First, you need to import the usePrevious
hook from the kitchn
package.
import { usePrevious } from "kitchn";
Example
Here is an example of how to use the usePrevious
hook in a component:
Parameters
The usePrevious
hook accepts the following parameters:
Name | Type | Description |
---|---|---|
state | T | The current state or prop whose previous value you want to track. |
Return Value
The usePrevious
hook returns a tuple with the following elements:
Name | Type | Description |
---|---|---|
prevState | T | undefined | null | The previous value of the state or prop passed to the hook. |