usePrevious
The usePrevious
hook allows you to keep track of the previous value of a state or prop in a React component. It stores all previous values in a Map
, which can be useful for debugging or tracking changes over time.
Add the utility
Parameters
value
: The current value to track.entriesKeysFormatter
(optional): A function that formats the key for each entry in the previous values map. Defaults to a function that returns the current timestamp.debug
(optional): A boolean that enables debug logging. Defaults tofalse
.
Return
This hook returns an object containing:
previousValue
: The previous value of the tracked state or prop.allPreviousValues
: An array of entries from the map containing all previous values, formatted as[key, value]
.