Skip to content

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.

count0
import {
import useStorage
useStorage
} from "@usels/web";
import {
class ObservablePersistLocalStorage
ObservablePersistLocalStorage
} from "@legendapp/state/persist-plugins/local-storage";
function
function Component(): any
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
() ? (
<
any
button
onClick: () => any
onClick
={() =>
const count$: any
count$
.
any
set
(
const count$: any
count$
.
any
get
() + 1)}>Count: {
const count$: any
count$
.
any
get
()}</
any
button
>
) : (
<
any
div
>Loading...</
any
div
>
)}
</>
);
}
import {
import useStorage
useStorage
} from "@usels/web";
import {
class ObservablePersistIndexedDB
ObservablePersistIndexedDB
} from "@legendapp/state/persist-plugins/indexeddb";
function
function Component(): any
Component
() {
const {
const data$: any
data$
,
const isPersistLoaded$: any
isPersistLoaded$
,
const error$: any
error$
} =
import useStorage
useStorage
(
"app-data",
{
items: {}
items
: [] },
{
plugin: typeof ObservablePersistIndexedDB
plugin
:
class ObservablePersistIndexedDB
ObservablePersistIndexedDB
,
}
);
return (
<
any
div
>
{
const error$: any
error$
.
any
get
() ? (
<
any
div
>Error: {
const error$: any
error$
.
any
get
()?.
any
message
}</
any
div
>
) :
const isPersistLoaded$: any
isPersistLoaded$
.
any
get
() ? (
<
any
div
>{
const data$: any
data$
.
any
items
.
any
get
().
any
length
} items</
any
div
>
) : (
<
any
div
>Loading storage...</
any
div
>
)}
</
any
div
>
);
}
export interface UseStorageOptions {
plugin: PersistOptions["plugin"];
transform?: PersistOptions["transform"];
retrySync?: PersistOptions["retrySync"];
readonly?: PersistOptions["readonly"];
options?: PersistOptions["options"];
}
export interface UseStorageReturn<T> {
data$: Observable<T>;
isLoaded$: ReadonlyObservable<boolean>;
isPersistLoaded$: ReadonlyObservable<boolean>;
error$: ReadonlyObservable<Error | undefined>;
}
export declare function useStorage<T>(key: string, defaults: T, options: UseStorageOptions): UseStorageReturn<T>;

View on GitHub

  • tigerwest
  • a7392ab 2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)