@anandarizki/use-undo-redo

中文文档
1.1.0发布于 2026/10/22MITTypeScriptCommonJS近 30 天下载 2924

A hook provides a simple and efficient way to manage undo and redo functionality in your React applications

原始使用文档
文档目录9 节
  1. Compatibility
  2. Demo
  3. Sponsor
  4. Features
  5. Installation
  6. Basic Usage
  7. Integrating into an Existing Component
  8. useUndoRedo
  9. Notes

use-undo-redo

The useUndoRedo hook provides a simple and efficient way to manage undo and redo functionality in your React applications. It helps you maintain a history of state changes, allowing users to seamlessly navigate through different states of your application.

Compatibility

React 18.2 or later, including React 19 ("react": ">=18.2.0 <20").

Demo

https://undoredo.rizki.id

Sponsor

JSpark Sponsored by JSpark

Features

  • Undo and Redo: Effortlessly navigate backward and forward through the state history.
  • Configurable History Capacity: Define the maximum number of state changes to keep in memory.
  • Debounce Support: Optimize performance by controlling the frequency of state history updates.
  • History Management: Access the full history stack, reset the history, or jump to a specific point.
  • Lightweight and Easy to Integrate: Minimal setup required to integrate into existing React components.

Installation

npm i @anandarizki/use-undo-redo

Basic Usage

//import the hook
import { useUndoRedo } from "@anandarizki/use-undo-redo";

//your state handler
const [state, setState] = useState();

//initialize hooks and pass your state and setState as the argument
const [undo, redo] = useUndoRedo([state, setState]);

Integrating into an Existing Component

You don't need to overhaul your existing state management. This hook can be seamlessly integrated into your code with just a single line addition.

Original component

import { useState } from "react";

function MyComponent() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <div>
        <button onClick={() => setCount(count - 1)}>-</button>
        {count}
        <button onClick={() => setCount(count + 1)}>+</button>
      </div>
    </div>
  );
}

Component with useUndoRedo

import { useState } from "react";
import { useUndoRedo } from "@anandarizki/use-undo-redo";

function MyComponent() {
  const [count, setCount] = useState(0);

  //new line
  const [undo, redo] = useUndoRedo([count, setCount]);

  return (
    <div>
      <div>
        <button onClick={() => setCount(count - 1)}>-</button>
        {count}
        <button onClick={() => setCount(count + 1)}>+</button>
      </div>

      <div>
        <button onClick={undo}>
          Undo
        </button>
        <button onClick={redo}>
          Redo
        </button>
      </div>
    </div>
  );
}

useUndoRedo

useUndoRedo<T>(primaryState: [T, (v: T) => void], options?: Options<T>): Output<T>
  • primaryState: An array containing the state value and the state setter function. This allows the hook to manage the history of this specific state.

  • options:

    • capacity (optional): The maximum number of entries kept in history, including the starting state, so capacity: 10 allows 9 undo steps. Values below 1 are treated as 1, and Infinity keeps everything. Default is 10.
    • debounce (optional): The time in milliseconds to debounce history updates. It is useful when your state updates frequently, i.e. when updating state using a controlled input. Calling undo or jumpTo while an update is pending records it first, so it can be redone. Default is 0 (no debounce).
    • clone (optional): How a state value is copied into history. true deep-clones it with lodash cloneDeep; false stores the value as-is, which is faster and uses less memory but is only safe when you never mutate state in place; a function (value) => copy lets you provide your own copy. Default is true.
  • Returns: An array containing:

    • undo: A function to revert to the previous state.
    • redo: A function to move forward to the next state.
    • An object with the following properties:
      • canUndo: A boolean indicating if an undo operation is possible.
      • canRedo: A boolean indicating if a redo operation is possible.
      • reset: A function to clear the history and reset the pointer.
      • history: An array of { value, timestamp } entries representing the current state history. Treat it as read-only.
      • pointer: The index of the current entry in history.
      • jumpTo: A function to jump to a specific point in the history by index. Invalid indexes are ignored.

undo, redo, jumpTo and reset keep the same identity across renders, so they are safe to pass to memoized components or use as effect dependencies.

The Options, Output and StateHistory types are exported for TypeScript users.

Notes

  • Changes are detected with a deep equality check (lodash isEqual). Setting a new object with the same content as the current entry is not recorded as a change.
  • Class instances with private (#field) properties can't be compared or cloned by lodash; store plain data instead.

Full usage

const [undo, redo, { canUndo, canRedo, jumpTo, history, pointer, reset }] =
  useUndoRedo([state, setState], {
    debounce: 500,
    capacity: 20,
  });

版本与发布记录

@anandarizki/use-undo-redo 共发布 11 个版本,首次发布于 2024/07/24;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.0latest2026/10/2216 天前
1.0.92025/09/201.1 年前
1.0.82025/09/201.1 年前
1.0.72024/07/262 年前
1.0.62024/07/262 年前
1.0.52024/07/262 年前

关于 @anandarizki/use-undo-redo 的常见问题

@anandarizki/use-undo-redo 是什么?

A hook provides a simple and efficient way to manage undo and redo functionality in your React applications

如何安装 @anandarizki/use-undo-redo?

在项目目录运行 npm install @anandarizki/use-undo-redo,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.2.0 <20)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@anandarizki/use-undo-redo 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anandarizki/use-undo-redo 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anandarizki/use-undo-redo 可以商用吗?

@anandarizki/use-undo-redo 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anandarizki/use-undo-redo 的下载量有多少?

根据 npm 官方下载统计,@anandarizki/use-undo-redo 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,924 次,日均约 97 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anandarizki/use-undo-redo 还在维护吗?

最新版本 1.1.0 发布于 2026/10/22(16 天前);至今共发布 11 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/07/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。