Skip to content

Getting Started

Terminal window
npm install @biswaviraj/time-travel
# or
pnpm add @biswaviraj/time-travel
import { timeTravel } from "@biswaviraj/time-travel";
const tt = timeTravel<number>(0);
tt.add(1);
tt.add(2);
tt.get(); // 2
tt.undo(); // returns 1
tt.redo(); // returns 2

You can pass an options object to configure the history limit. The default limit is 10.

const tt = timeTravel<string>("hello", { limit: 20 });

The limit controls the maximum number of past and future states kept in memory. Once exceeded, the oldest entries are dropped.

A React hook is available as a separate import:

import { useTimeTravel } from "@biswaviraj/time-travel/react";
function Counter() {
const { state, add, undo, redo, canUndo, canRedo } = useTimeTravel(0);
return (
<div>
<p>Count: {state}</p>
<button onClick={() => add(state + 1)}>Increment</button>
<button onClick={() => undo()} disabled={!canUndo}>Undo</button>
<button onClick={() => redo()} disabled={!canRedo}>Redo</button>
</div>
);
}

The hook returns all core API methods plus a reactive state property that triggers re-renders. Requires React 18+.

See the full React API reference for details.