MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useLocalStorage

Function useLocalStorage

packages/dev/s2-docs/src/useLocalStorage.ts:3–10  ·  view source on GitHub ↗
(
  key: string,
  defaultValue: string
)

Source from the content-addressed store, hash-verified

1import {useState, useSyncExternalStore} from 'react';
2
3export function useLocalStorage(
4 key: string,
5 defaultValue: string
6): [string, (value: string) => void] {
7 let [store] = useState(() => new Store(key, defaultValue));
8 let value = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot);
9 return [value, store.setValue];
10}
11
12class Store {
13 key: string;

Callers 5

ExampleSwitcherFunction · 0.90
useSettingsStateFunction · 0.90
InstallCommandFunction · 0.90
ShadcnCommandFunction · 0.90
BundlerSwitcherFunction · 0.90

Calls 1

useSyncExternalStoreFunction · 0.90

Tested by

no test coverage detected