useUndoInformation
The useUndoInformation
hook returns an UndoOrRedoInformation
array that indicates if and how you can move the state of the underlying Store
backward to the previous checkpoint.
useUndoInformation(checkpointsOrCheckpointsId?: CheckpointsOrCheckpointsId): UndoOrRedoInformation
Type | Description | |
---|---|---|
checkpointsOrCheckpointsId? | CheckpointsOrCheckpointsId | The |
returns | UndoOrRedoInformation |
|
This hook is useful if you are building an undo button: the information contains whether an undo action is available (to enable the button), the callback to perform the undo action, the current checkpoint Id
that will be undone, and its label, if available.
Example
This example uses the useUndoInformation
hook to create an undo button.
import {createCheckpoints, createStore} from 'tinybase';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {useUndoInformation} from 'tinybase/ui-react';
const store = createStore().setTables({pets: {nemo: {species: 'fish'}}});
const checkpoints = createCheckpoints(store);
const App = () => {
const [canUndo, handleUndo, _id, label] =
useUndoInformation(checkpoints);
return canUndo ? (
<span onClick={handleUndo}>Undo {label}</span>
) : (
<span>Nothing to undo</span>
);
};
const app = document.createElement('div');
createRoot(app).render(<App />);
console.log(app.innerHTML);
// -> '<span>Nothing to undo</span>'
store.setCell('pets', 'nemo', 'color', 'orange');
checkpoints.addCheckpoint('color');
console.log(app.innerHTML);
// -> '<span>Undo color</span>'
Since
v1.0.0