Skip to content
0

katanakit-js / useCreateStorageSignal

Function: useCreateStorageSignal() ​

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

Defined in: src/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 Parameters ​

Type ParameterDescription
TThe signal value type.

Parameters ​

ParameterTypeDefault valueDescription
keystringundefinedStorage key.
fallbackValueTundefinedValue used when the key is not in storage.
target"localStorage" | "sessionStorage""localStorage"Storage backend (defaults to "localStorage").

Returns ​

[SignalGetter<T>, SignalSetter<T>]

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

Example ​

ts
import { useCreateStorageSignal } from "katanakit-js";

const [theme, setTheme] = useCreateStorageSignal("theme", "dark");
theme(); // "dark" (or whatever was stored)
setTheme("light"); // persists to localStorage

Released under the MIT License.