We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1544815 commit 0ba6343Copy full SHA for 0ba6343
src/components/useUndoRedo.ts
@@ -1,9 +1,15 @@
1
-import { useState } from 'react';
+import { useState,useEffect } from 'react';
2
function useUndoRedo<T>(initialValue: T, onChange?: (value: T) => void) {
3
const [past, setPast] = useState<T[]>([]);
4
const [present, setPresent] = useState<T>(initialValue);
5
const [future, setFuture] = useState<T[]>([]);
6
7
+ useEffect(() => {
8
+ setPresent(initialValue);
9
+ setPast([]);
10
+ setFuture([]);
11
+ }, [initialValue]);
12
+
13
const setValue = (newValue: T) => {
14
setPast((prevPast) => [...prevPast, present]);
15
setPresent(newValue);
0 commit comments