Skip to main content

Variable: usePrevious

const usePrevious: UsePrevious

Defined in: use-previous.ts:38

Returns the value passed to the hook on the previous render.

Useful for diffing props or state, detecting direction of change, or driving animations. It does not trigger extra re-renders: the value is recorded in a ref after each commit.

Without initialValue the hook returns undefined on the first render. To track a derived value (for example item.id) pass that value instead of the whole object. Read the result while rendering or in effects.

Param​

value

The value to track.

Param​

options

Optional settings; initialValue is returned on the first render.

Returns​

The value from the previous render.

Example​

const prevCount = usePrevious(count);
const direction = prevCount === undefined || count === prevCount
? "none"
: count > prevCount ? "up" : "down";