useHistoryState

Manage state with history (undo/redo).

https://www.shaktools.com/shak-hooks
State

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useHistoryState } from "@shak-hooks/usehooks";
Source: packages/react/src/useHistoryState.ts

Signature

signature.ts
export function useHistoryState<T>(initialValue: T, key: string)

Parameters

NameTypeOptionalDefault
initialValueTNo-
keystringNo-

Returns

unknown

Implementation

use-history-state.ts
1import { useState, useCallback } from "react";
2
3export function useHistoryState<T>(initialValue: T) {
4 const [state, setState] = useState(initialValue);
5 const [history, setHistory] = useState<T[]>([initialValue]);
6 const [pointer, setPointer] = useState(0);
7
8 const set = useCallback((value: T) => {
9 const nextHistory = [...history.slice(0, pointer + 1), value];
10 setHistory(nextHistory);
11 setPointer(nextHistory.length - 1);
12 setState(value);
13 }, [history, pointer]);
14
15 const undo = useCallback(() => {
16 if (pointer > 0) {
17 setPointer((p) => p - 1);
18 setState(history[pointer - 1]);
19 }
20 }, [history, pointer]);
21
22 const redo = useCallback(() => {
23 if (pointer < history.length - 1) {
24 setPointer((p) => p + 1);
25 setState(history[pointer + 1]);
26 }
27 }, [history, pointer]);
28
29 return { state, set, undo, redo, canUndo: pointer > 0, canRedo: pointer < history.length - 1 };
30}

Advertisement

Google Ads

Usage

example.tsx
1import { useHistoryState } from "@shak-hooks/usehooks";
2
3const result = useHistoryState(undefined, "...");
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy