shadcn-ahooks

usePrevious

A Hook to return the previous state.

Overview

A Hook to return the previous state.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/usePrevious.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/usePrevious.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/usePrevious.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/usePrevious.json

A Hook to return the previous state.

Examples

Default usage

Custom shouldUpdate function

API

const previousState: T = usePrevious<T>(
  state: T,
  shouldUpdate?: (prev: T | undefined, next: T) => boolean
);

Result

| Property | Description | Type | | - | | - | | state | The state that needs to be tracked | T | - | | shouldUpdate | Optional. Customize whether the state value should be updated | (prev: T \| undefined, next: T) => boolean | (a, b) => !Object.is(a, b) |

On this page