Getting Started
Installation
Section titled “Installation”npm install @biswaviraj/time-travel# orpnpm add @biswaviraj/time-travelBasic Usage
Section titled “Basic Usage”import { timeTravel } from "@biswaviraj/time-travel";
const tt = timeTravel<number>(0);
tt.add(1);tt.add(2);
tt.get(); // 2tt.undo(); // returns 1tt.redo(); // returns 2Options
Section titled “Options”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.