Shadcn Hooks

usePrevious

A hook to get the previous value of a variable

Loading...

Installation

npx shadcn@latest add @shadcnhooks/use-previous

API

export type ShouldUpdateFunc<T> = (prev?: T, next?: T) => boolean

const defaultShouldUpdate = <T>(a?: T, b?: T) => !Object.is(a, b)
/**
 * A hook to get the previous value of a state
 * @param state - The state to get the previous value of
 * @param shouldUpdate - The function to determine if the state should be updated
 * @returns The previous value of the state
 */
export function usePrevious<T>(
  state: T,
  shouldUpdate?: ShouldUpdateFunc<T>,
): T | undefined

Credits

Last updated on

On this page