npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-use-history

v0.1.1

Published

Undo/redo history for React — drop-in useState upgrade with coalescing, transactions, and labels

Readme

react-use-history

Drop-in useState upgrade with undo/redo, coalescing, transactions, pause/resume, time-travel, and keyboard shortcuts.

Install

npm install react-use-history

Requires React 18+

Quick start

import { useHistory } from 'react-use-history';

function Editor() {
  const { state, set, undo, redo, canUndo, canRedo } = useHistory('');

  return (
    <>
      <textarea value={state} onChange={(e) => set(e.target.value)} />
      <button onClick={undo} disabled={!canUndo}>Undo</button>
      <button onClick={redo} disabled={!canRedo}>Redo</button>
    </>
  );
}

That's it. set works exactly like the setter from useState.


Features

Coalescing — merge rapid changes into one step

const { state, set } = useHistory('', { coalesce: 800 });
// keystrokes within 800ms are merged into one undo step

// or use a custom predicate:
useHistory(value, { coalesce: (prev, next) => prev.type === next.type });

Labeled entries

set(newValue, { label: 'renamed layer' });

Labels show up in history.past so you can render a named history panel.

Transactions — group multiple sets into one undo step

const { set, transaction } = useHistory(items);

transaction(() => {
  set((prev) => deleteItem(prev, id));
  set((prev) => renumber(prev));
}); // undo reverses both at once

Pause / Resume — useful for drag operations

const { pause, resume, set } = useHistory(position);

onMouseDown={() => pause()}   // start drag — changes won't be recorded
onMouseUp={() => resume()}    // end drag — one undo step covers the whole drag

Time-travel — jump to any point in history

const { goto, history } = useHistory(color);

// history.timeline = [...past, present, ...future]
// history.index    = index of present in timeline

history.timeline.map((entry, i) => (
  <button
    key={i}
    onClick={() => goto(i)}
    style={{ fontWeight: i === history.index ? 'bold' : 'normal' }}
  >
    {entry.label ?? `Step ${i + 1}`}
  </button>
));

Keyboard shortcuts

import { useUndoShortcuts } from 'react-use-history';

const { undo, redo, canUndo, canRedo } = useHistory(state);

useUndoShortcuts({ undo, redo, canUndo, canRedo });
// Cmd+Z → undo, Cmd+Shift+Z / Cmd+Y → redo

History limit

useHistory(value, { limit: 50 }); // keep at most 50 past entries (default: 100)

API

useHistory(initialState, options?)

| Return value | Type | Description | |---|---|---| | state | T | Current value | | set | (value, opts?) => void | Update state (records history) | | undo | () => void | Step back | | redo | () => void | Step forward | | goto | (index: number) => void | Jump to any timeline position | | canUndo | boolean | Whether undo is available | | canRedo | boolean | Whether redo is available | | clear | () => void | Reset to initial state, clear history | | pause | () => void | Stop recording | | resume | () => void | Resume recording | | transaction | (fn: () => void) => void | Group sets into one step | | history.past | HistoryEntry<T>[] | Past entries (oldest → newest) | | history.present | HistoryEntry<T> | Current entry | | history.future | HistoryEntry<T>[] | Future entries (if any) | | history.index | number | Index of present in timeline | | history.timeline | HistoryEntry<T>[] | [...past, present, ...future] |

Options:

| Option | Type | Default | Description | |---|---|---|---| | limit | number | 100 | Max past entries to keep | | coalesce | number \| (prev, next) => boolean | — | Merge threshold in ms, or custom predicate |

useUndoShortcuts({ undo, redo, canUndo?, canRedo?, enabled? })

Binds Cmd+Z (undo) and Cmd+Shift+Z / Cmd+Y (redo) to the window. Pass enabled: false to disable.

createHistoryStore(initialState, options?)

The headless store — same interface as above but usable outside React (e.g. in Zustand, a plain module, or tests).


License

MIT