valtio-history
v1.0.0
Published
valtio utility for creating a proxy state with history tracking
Downloads
3,429
Readme
valtio-history
valtio utility for creating a proxy state with history tracking
https://valtio.pmnd.rs/docs/api/utils/proxyWithHistory
see detailed api docs for more info.
Migrating from valtio/utils
// replace the following line
// import { proxyWithHistory } from 'valtio/utils';
import { proxyWithHistory } from 'valtio-history';
import { useSnapshot } from 'valtio';
const state = proxyWithHistory({ count: 0 });
console.log(state.value); // ---> { count: 0 }
state.value.count += 1;
console.log(state.value); // ---> { count: 1 }
state.undo();
console.log(state.value); // ---> { count: 0 }
state.redo();
console.log(state.value); // ---> { count: 1 }
// React example
export default function App() {
const {
value,
undo,
redo,
history,
isUndoEnabled,
isRedoEnabled,
currentChangeDate,
remove,
replace,
} = useSnapshot(state);
...
}
Notable changes
- the
history
object has changeshistory.snapshots
is renamed tohistory.nodes
- a
HistoryNode
has the structure{ snapshot: Snapshot<T>; createdAt: Date; updatedAt?: Date; }
- The second parameter of
proxyWithHistory
is now an object instead of aboolean
.{ skipSubscribe?: boolean }