Use `useTemporalStore` for reactive temporal state
mainIf you need your React components to re-render when the history changes (e.g., to show the number of past states or disable an undo button when no history exists), use a custom hook built on useStoreWithEqualityFn to subscribe to the temporal object.
This makes properties like pastStates and futureStates reactive.
import { useStoreWithEqualityFn } from 'zustand/traditional';
import type { TemporalState } from 'zundo';
// Implementation of the reactive hook
function useTemporalStore<T>(selector: (state: TemporalState<MyState>) => T): T {
return useStoreWithEqualityFn(useStoreWithUndo.temporal, selector!);
}
const App = () => {
const { bears, increasePopulation } = useStoreWithUndo();
// This hook makes pastStates and futureStates reactive
const { undo, redo, clear, pastStates, futureStates } = useTemporalStore(
(state) => state,
);
return (
<>
<p>bears: {bears}</p>
<p>pastStates: {JSON.stringify(pastStates)}</p>
<button onClick={increasePopulation}>increase</button>
<button onClick={() => undo()}>undo</button>
</>
);
};