Skip to main content

API Reference

The library exports the following:

  • useStateTimeline — the main hook to manage state history
  • StateTimelineDevTools — an optional UI panel to inspect the timeline visually

useStateTimeline hook

useStateTimeline<T>(initialState: T)

Description

Creates a state with full timeline tracking — including undo, redo, and direct navigation between versions.

Returns

NameTypeDescription
stateTCurrent state value.
setState(newState: T | (prev: T) => T) => voidUpdates the state and records it in the timeline.
timeline{ value: T; date: Date }[]Full timeline of states with timestamps.
currentIndexnumberThe current position in the timeline.
goTo(index: number) => voidJump to a specific index in the timeline.
undo() => voidMove one step backward in the timeline.
redo() => voidMove one step forward in the timeline.
canUndobooleanWhether an undo is possible.
canRedobooleanWhether a redo is possible.
reset() => voidReset to the initial state and clear the timeline.

StateTimelineDevTools component

<StateTimelineDevTools>

Description

A floating debug panel that displays and controls the timeline recorded by useStateTimeline.
It can be toggled open or closed and allows you to inspect, scroll through, and jump to previous states directly from the UI.

Props

NameTypeRequiredDescription
timelineUseStateTimelineReturn<T>['timeline']✅ YesThe timeline array returned from useStateTimeline.
currentIndexUseStateTimelineReturn<T>['currentIndex']❌ NoThe current index of the state within the timeline.
goToUseStateTimelineReturn<T>['goTo']❌ NoFunction to navigate to a specific state when a timeline item is clicked.