travels3.0
Guides

Navigation and Controls

Move through history and expose the right read-only capabilities to UI components.

Undo, redo, and jumps

travels.back();       // One entry
travels.back(3);      // Three entries
travels.forward();    // One entry
travels.forward(2);   // Two entries
travels.go(0);        // Retained window start

Use canBack() and canForward() for button availability. getPosition() gives the cursor and each subscriber event carries the retained step count in historyLength.

back() and forward() publish navigation as go events. Manual pending changes are implicitly archived as part of a successful navigation; Travels 3 publishes one settled go event rather than separate archive and go events.

Reset and rebase are different

  • reset() returns to the current instance baseline.
  • rebase() discards all history and makes current state the new baseline.

Both are timeline mutations and notify observers. Neither means “go to the oldest retained patch” in every situation; read History Lifecycle before exposing them as product actions.

Narrow controls for components

getControls() returns a cached, frozen facade suitable for buttons, menus, and component props:

const controls = travels.getControls();

function HistoryButtons() {
  useSyncExternalStore(travels.subscribe, travels.getRevision);

  return (
    <>
      <button disabled={!controls.canBack()} onClick={() => controls.back()}>Undo</button>
      <button disabled={!controls.canForward()} onClick={() => controls.forward()}>Redo</button>
      <span>{controls.position}</span>
    </>
  );
}

The facade includes navigation, current position, read-only patch/history views, and rebase(). Manual timelines expose manual-aware control types. State-owning methods such as setState() are not included.

Re-render on revision, not only state identity

archive() and rebase() can change controls without changing the current state reference. getRevision() advances once for every committed subscriber event, making it the correct snapshot for useSyncExternalStore.

Shared views versus owned snapshots

APIOwnershipUse it for
getHistory()Shared cached read-only array and state viewsImmediate timeline rendering and reads
getHistorySnapshot()Detached caller-owned statesKeeping, sorting, editing, worker transfer
getPatches()Detached retained patch dataOwned copies, custom serialization, analysis
controls.patchesShared cached read-only viewRender-time reads and memo dependencies
getHistoryEntries()Patch entries with inverse patches and metadataUndo menus, devtools, audit views

Never cast away read-only types to modify a shared cache. Mutable timelines can also update an old shared history view in place; use detached snapshots when a historical value must remain unchanged.

Metadata-driven undo menus

const entries = travels.getHistoryEntries();
const current = travels.getPosition();
const previous = entries[current - 1];
const next = entries[current];

const undoLabel = previous?.metadata?.label ?? 'Undo';
const redoLabel = next?.metadata?.label ?? 'Redo';

Metadata indexes align with retained patch entries. When old history is trimmed, labels for trimmed entries disappear with them; derive menu text from the current retained entries rather than a separate unbounded UI list.

On this page