getPrevious
Category Reactivity
A reactive state of a given state's previous value.
It is set to undefined
until the first change if initial
is not set.
Demo
Counter : 0
Previous counter : undefined
Usage
TIP
If you only care about the previous value when the value changes, you can use watch.
It supplies the previous value in the callback.
<script>
import { getPrevious } from '@sv-use/core';
let counter = $state(0);
let previousCounter = getPrevious(() => counter);
</script>
Type Definitions
type GetPreviousReturn<T> = {
current: T;
};
/**
* A reactive state of a given state's previous value.
* @param getter The state as a getter function.
* @note The state is `undefined` until the given state is updated for the first time.
* @see https://svelte-librarian.github.io/sv-use/docs/get-previous
*/
export declare function getPrevious<T>(getter: () => T): GetPreviousReturn<T | undefined>;
/**
* A reactive state of a given state's previous value.
* @param getter The state as a getter function.
* @param initial The initial value of the state.
* @see https://svelte-librarian.github.io/sv-use/docs/get-previous
*/
export declare function getPrevious<T>(getter: () => T, initial: T): GetPreviousReturn<T>;
export {};