Skip to content

UseCreateStorageSignal

katanakit-js / useCreateStorageSignal

function useCreateStorageSignal<T>(
key,
fallbackValue,
target?,
): [SignalGetter<T>, SignalSetter<T>];

Defined in: infrastructure/storage/storage-signal.service.ts:32

Creates a signal that persists its value to a storage backend (localStorage, sessionStorage, etc.). Reads the initial value from storage on creation.

This lives in the infrastructure layer, not in core, because it is the one reactive helper that performs real I/O. Core stays free of runtime I/O; the pure signal primitives (useCreateSignal, useCreateEffect, useCreateMemo, …) remain in core/services/reactive.service.ts.

Type Parameter Description
T The signal value type.
Parameter Type Default value Description
key string undefined Storage key.
fallbackValue T undefined Value used when the key is not in storage.
target? "localStorage" | "sessionStorage" "localStorage" Storage backend (defaults to "localStorage").

[SignalGetter<T>, SignalSetter<T>]

A tuple of [getter, setter]. The setter writes through to storage.

import { useCreateStorageSignal } from "katanakit-js";
const [theme, setTheme] = useCreateStorageSignal("theme", "dark");
theme(); // "dark" (or whatever was stored)
setTheme("light"); // persists to localStorage