|
| 1 | +# useHistory |
| 2 | + |
| 3 | +`useHistory` is a React hook that tracks the change history of a value and provides undo/redo functionality. Each time `setValue` is called, the value is recorded in the history stack, and you can navigate through past values using `undo` and `redo`. |
| 4 | + |
| 5 | +## Interface |
| 6 | + |
| 7 | +```ts |
| 8 | +function useHistory<T>( |
| 9 | + initialValue: T, |
| 10 | + options?: { capacity?: number } |
| 11 | +): { |
| 12 | + value: T; |
| 13 | + setValue: (value: T) => void; |
| 14 | + history: readonly T[]; |
| 15 | + pointer: number; |
| 16 | + undo: () => void; |
| 17 | + redo: () => void; |
| 18 | + canUndo: boolean; |
| 19 | + canRedo: boolean; |
| 20 | + clear: () => void; |
| 21 | +}; |
| 22 | +``` |
| 23 | + |
| 24 | +### Parameters |
| 25 | + |
| 26 | +<Interface |
| 27 | + name="initialValue" |
| 28 | + type="T" |
| 29 | + description="The initial value to track." |
| 30 | +/> |
| 31 | + |
| 32 | +<Interface |
| 33 | + name="options" |
| 34 | + type="{ capacity?: number }" |
| 35 | + description="Configuration options." |
| 36 | + :nested="[ |
| 37 | + { |
| 38 | + name: 'capacity', |
| 39 | + type: 'number', |
| 40 | + required: false, |
| 41 | + description: 'The maximum number of history entries to keep. When exceeded, the oldest entry is removed. Defaults to unlimited.', |
| 42 | + }, |
| 43 | + ]" |
| 44 | +/> |
| 45 | + |
| 46 | +### Return Value |
| 47 | + |
| 48 | +<Interface |
| 49 | + name="" |
| 50 | + type="object" |
| 51 | + description="An object containing:" |
| 52 | + :nested="[ |
| 53 | + { |
| 54 | + name: 'value', |
| 55 | + type: 'T', |
| 56 | + required: false, |
| 57 | + description: 'The current value.', |
| 58 | + }, |
| 59 | + { |
| 60 | + name: 'setValue', |
| 61 | + type: '(value: T) => void', |
| 62 | + required: false, |
| 63 | + description: 'A function to set a new value and record it in the history.', |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'history', |
| 67 | + type: 'readonly T[]', |
| 68 | + required: false, |
| 69 | + description: 'A readonly array of all recorded values.', |
| 70 | + }, |
| 71 | + { |
| 72 | + name: 'pointer', |
| 73 | + type: 'number', |
| 74 | + required: false, |
| 75 | + description: 'The current position in the history stack.', |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'undo', |
| 79 | + type: '() => void', |
| 80 | + required: false, |
| 81 | + description: 'A function to revert to the previous value.', |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'redo', |
| 85 | + type: '() => void', |
| 86 | + required: false, |
| 87 | + description: 'A function to move forward to the next value.', |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'canUndo', |
| 91 | + type: 'boolean', |
| 92 | + required: false, |
| 93 | + description: 'Whether there is a previous value to revert to.', |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'canRedo', |
| 97 | + type: 'boolean', |
| 98 | + required: false, |
| 99 | + description: 'Whether there is a next value to move forward to.', |
| 100 | + }, |
| 101 | + { |
| 102 | + name: 'clear', |
| 103 | + type: '() => void', |
| 104 | + required: false, |
| 105 | + description: 'A function to clear the history and reset to the current value.', |
| 106 | + }, |
| 107 | + ]" |
| 108 | +/> |
| 109 | + |
| 110 | +## Example |
| 111 | + |
| 112 | +```tsx |
| 113 | +function TextEditor() { |
| 114 | + const { value, setValue, undo, redo, canUndo, canRedo } = useHistory(''); |
| 115 | + |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + <input value={value} onChange={e => setValue(e.target.value)} /> |
| 119 | + <button onClick={undo} disabled={!canUndo}> |
| 120 | + Undo |
| 121 | + </button> |
| 122 | + <button onClick={redo} disabled={!canRedo}> |
| 123 | + Redo |
| 124 | + </button> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +```tsx |
| 131 | +function Counter() { |
| 132 | + const { value, setValue, undo, canUndo, clear } = useHistory(0, { |
| 133 | + capacity: 10, |
| 134 | + }); |
| 135 | + |
| 136 | + return ( |
| 137 | + <div> |
| 138 | + <p>Count: {value}</p> |
| 139 | + <button onClick={() => setValue(value + 1)}>Increment</button> |
| 140 | + <button onClick={undo} disabled={!canUndo}> |
| 141 | + Undo |
| 142 | + </button> |
| 143 | + <button onClick={clear}>Clear History</button> |
| 144 | + </div> |
| 145 | + ); |
| 146 | +} |
| 147 | +``` |
0 commit comments