shadcn-ahooks

useHistoryTravel

A hook to manage state change history. It provides encapsulation methods to travel through the history.

Overview

A hook to manage state change history. It provides encapsulation methods to travel through the history.

Documentation and Examples

Installation

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

A hook to manage state change history. It provides encapsulation methods to travel through the history.

Examples

Basic usage

Todo List

Limit maximum history length

API

const {
  value,
  setValue,
  backLength,
  forwardLength,
  go,
  back,
  forward
} = useHistoryTravel<T>(initialValue?: T, maxLength: number = 0 );

Params

PropertyDescriptionTypeDefault
initialValueOptional initial valueT-
maxLengthOptional limit the maximum length of history records. If the maximum length is exceeded, the first record will be deletednumber0 unlimited

Result

PropertyDescriptionType
valueCurrent valueT
setValueSet value(value: T) => void
backLengthThe length of backward historynumber
forwardLengthThe length of forward historynumber
goMove between the history, move backward on step < 0,and move forward on step > 0(step: number) => void
backMove one step backward() => void
fowardMove one step forward() => void
resetReset history to initial value by default or provide a new initial value.(newInitialValue?: T) => void

On this page