useStorage
Reactive storage binding powered by Legend-State's persist & sync engine.
Creates an Observable<T> that automatically persists to a storage backend. The plugin option is required — use useLocalStorage or useSessionStorage from @usels/web for convenience wrappers with pre-configured plugins.
useStorage
Value persists across page reloads via localStorage.
0
import { import useStorage
useStorage } from "@usels/web";import { class ObservablePersistLocalStorage
ObservablePersistLocalStorage } from "@usels/web";
function function Component(): JSX.Element
Component() { const { any
data$: const count$: any
count$, const isPersistLoaded$: any
isPersistLoaded$ } = import useStorage
useStorage("count", 0, { plugin: typeof ObservablePersistLocalStorage
plugin: class ObservablePersistLocalStorage
ObservablePersistLocalStorage, });
return ( <> {const isPersistLoaded$: any
isPersistLoaded$.any
get() ? ( <JSX.IntrinsicElements.button: DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>
button DOMAttributes<HTMLButtonElement>.onClick?: MouseEventHandler<HTMLButtonElement> | undefined
onClick={() => const count$: any
count$.any
set(const count$: any
count$.any
get() + 1)}>Count: {const count$: any
count$.any
get()}</JSX.IntrinsicElements.button: DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>
button> ) : ( <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>Loading...</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> )} </> );}import { createStorage, observable } from "@usels/web";import { ObservablePersistLocalStorage } from "@usels/web";
function Component() { "use scope" const { data$: count$, isPersistLoaded$ } = createStorage("count", 0, { plugin: ObservablePersistLocalStorage, });
return ( <> {isPersistLoaded$.get() ? ( <button onClick={() => count$.set(count$.get() + 1)}>Count: {count$.get()}</button> ) : ( <div>Loading...</div> )} </> );}Async plugin (IndexedDB)
Section titled “Async plugin (IndexedDB)”import { useStorage } from "@usels/web";import { ObservablePersistIndexedDB } from "@usels/web";
function Component() { const { data$, isPersistLoaded$, error$ } = useStorage( "app-data", { items: [] }, { plugin: ObservablePersistIndexedDB } );
return ( <div> {error$.get() ? ( <div>Error: {error$.get()?.message}</div> ) : isPersistLoaded$.get() ? ( <div>{data$.items.get().length} items</div> ) : ( <div>Loading storage...</div> )} </div> );}import { createStorage, observable } from "@usels/web";import { ObservablePersistIndexedDB } from "@usels/web";
function Component() { "use scope" const { data$, isPersistLoaded$, error$ } = createStorage( "app-data", { items: [] }, { plugin: ObservablePersistIndexedDB } );
return ( <div> {error$.get() ? ( <div>Error: {error$.get()?.message}</div> ) : isPersistLoaded$.get() ? ( <div>{data$.items.get().length} items</div> ) : ( <div>Loading storage...</div> )} </div> );}export { createStorage } from "./core";export type { StorageOptions, StorageReturn } from "./core";/** * Reactive storage binding powered by Legend-State's * [persist & sync](https://legendapp.com/open-source/state/v3/sync/persist-sync/) engine. * * Creates an `Observable<T>` that automatically persists to a storage backend. * * @param key - Storage key (maps to `persist.name`). * @param defaults - Initial value and type inference source. * @param options - Configuration options. `plugin` is required. * @returns `{ data$, isPersistLoaded$, error$ }` * * @example * ```ts * import { ObservablePersistLocalStorage } from "@legendapp/state/persist-plugins/local-storage"; * * const { data$: count$, isPersistLoaded$ } = useStorage('count', 0, { * plugin: ObservablePersistLocalStorage, * }); * data$.set(42); // persisted to storage * data$.get(); // 42 * ``` */export type UseStorage = typeof createStorage;export declare const useStorage: UseStorage;