Skip to content
Sync

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 { 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>
);
}
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;

View on GitHub