historyState

Category States

A reactive state that tracks its change history. Provides undo and redo capabilities as well as access to the histories.

Demo


Search history

Empty...

Usage

TIP

If you prefer to have them separate, check out trackHistory.

<script>
	import { historyState } from '@sv-use/core';
 
	const counter = historyState(0);
</script>

Examples

<script>
	import { historyState } from '@sv-use/core';
 
	const counter = historyState(0);
</script>
 
<span>counter : {counter.current}</span>
<span>change history : {JSON.stringify(counter.history, null, 2)}</span>
<button onclick={() => counter.current++}> Increment </button>
<button onclick={() => counter.current--}> Decrement </button>
Type definitions
import { type TrackHistoryOptions, type TrackHistoryReturn } from '../../reactivity/index.js';
type HistoryStateOptions = TrackHistoryOptions;
type HistoryStateReturn<T> = TrackHistoryReturn<T> & {
    current: T;
};
/**
 * A reactive state that allows for undo and redo operations by tracking the change history.
 * @param initial The initial value of the state.
 * @see https://svelte-librarian.github.io/sv-use/docs/core/states/history-state
 */
export declare function historyState<T>(initial: T, options?: HistoryStateOptions): HistoryStateReturn<T>;
export {};

Sources