usePrevious

Track the previous value of a variable with usePrevious.

Install:

npm i @uidotdev/usehooks

Description:

The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.

Parameters

NameTypeDescription
newValueanyThe new value to track and return the previous of.
initialValueany(Optional) The initial Previous Value. Default value is null.

Return Value

NameTypeDescription
previousValueanyThe previous value of the provided newValue.

Demo:

Example:

More Hooks:

Sort: