defaultState

Category States

A reactive state that falls back to defaultValue if set to null or undefined.

Usage

NOTE

Although the current property is typed as nullable, it will never return a nullable value.

This is to ensure that you can set a nullable value when changing the state without TS complaining.

<script>
    import { defaultState } from "@sv-use/core";
 
    const message = defaultState("fallback message", "initial message");
 
    message.current = "Hello, World!";
 
    message.current = null; // message is now "fallback message"
</script>
Type definitions
type DefaultStateReturn<T> = {
    /**
     * @note Although it is typed as nullable, reading the value will never return a nullable value.
     *
     * This is to ensure that you can set a nullable value when changing the state without TS complaining.
     */
    current: T | null | undefined;
};
/**
 * A reactive state that falls back to `defaultValue` if set to `null` or `undefined`.
 * @param defaultValue The fallback value when the value is set to `null` or `undefined`.
 * @param initialValue The initial value of the state. Defaults to `defaultValue` if omitted.
 * @see https://svelte-librarian.github.io/sv-use/docs/core/states/default-state
 */
export declare function defaultState<T>(defaultValue: T, initialValue?: T): DefaultStateReturn<T>;
export {};

Sources